diff --git a/.gitignore b/.gitignore index 14bc68c..6c39d04 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/nbproject/private/ \ No newline at end of file +/nbproject/private/ +/node_modules/ +/coverage/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 7dea91e..2728d75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,12 @@ -script: phantomjs test/phantomRunner.js test/index.html \ No newline at end of file +language: node_js +node_js: +- '0.11' +- '0.10' +- '0.8' +deploy: + provider: npm + api_key: + secure: iHcy2J1AKnPb/LzaGCIr6ziELKqXL4jPTmGpgQfzng79bsfDIi7QhSXL6IANzlqswU/VKB2eUrFBlvEC/MJ5YmbdovzX+uELexhp5GCeOBv3lSJQWVgFU3NKFM6vje4iBcBjqUg+IG4gZs0NI17YWd9zfDAY9AKvl2UoCEo9R18= + on: + tags: true + node: 0.10 diff --git a/README.md b/README.md index e8f19d7..7f87634 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # MeasurementJs - +[![Code Climate](https://codeclimate.com/github/Philzen/measurement.js.png)] +(https://codeclimate.com/github/Philzen/measurement.js) [![Build Status](https://travis-ci.org/Philzen/measurement.js.svg?branch=0-1-stable)] (https://travis-ci.org/Philzen/measurement.js) [![devDependency Status](https://david-dm.org/philzen/measurement.js/dev-status.svg?theme=shields.io)] (https://david-dm.org/philzen/measurement.js#info=devDependencies) -[![Code Climate](https://codeclimate.com/github/Philzen/measurement.js.png)] -(https://codeclimate.com/github/Philzen/measurement.js) + Nice unit of measure conversion, featuring: - __Simplicity__: an easy to-use, Behaviour driven API -- __Sophistication__: full test coverage from project day one +- __Sophistication__: [![Coverage Status](https://coveralls.io/repos/Philzen/measurement.js/badge.png?branch=master)](https://coveralls.io/r/Philzen/measurement.js?branch=master) full test coverage from project day one - __Quality__: aiming at high performance whilst maintaining a fair trade-off between accuracy - __Adaptability__: Easy to extend for new measurement types (incl. i18n tables) @@ -34,14 +34,20 @@ measurement('Speed').convert(10) The API definition and all conversion operations are covered by jasmine tests. The test suite can be executed straightaway and easily, for example: - - In the Browser: -Simply open test/index.html in the browser environment you want to test +- Test directly in the browser +Simply open test/index.html in the browser - - CLI-based (using npm) -`npm test` + - PhantomJs via Karma test driver +`npm run-script karma` +That command is just a shorthand for `node_modules/.bin/karma start`. If you have +`karma-cli` already installed globally, you can also just do `karma start`. +Karma will startup and run all tests on phantomjs - - CLI-based (w/o npm, you will need to ensure phantomjs is available yourself) -`phantomjs test/phantomRunner.js test/index.html` +- Any Browser you like with Karma + 1. Set `singleRun:false` in `./karma.conf.js` + 2. Start the Karma with `npm run-script karma` or `npm test` or ... (see above) + 3. navigate any browser you'd like to test to http://localhost:9876 + 2. observe the output on the test console ### Roadmap @@ -51,10 +57,22 @@ For current and future state of affairs, have a peek at the [Roadmap](ROADMAP.md Currently you only need to download and include [measurement.js](https://raw.githubusercontent.com/Philzen/measurement.js/0-1-stable/measurement.js) in your project. +latest release - install via NPM: + In case the node package manager is part of your development stack, this may be more comfortable for you: [![NPM](https://nodei.co/npm/measurementjs.png?downloads=true&stars=true)](https://www.npmjs.org/package/measurementjs) + +### Currently tested platforms + +Please feel free to add your own test results. + + PhantomJS 1.9.7 (Linux): Executed 46 of 46 SUCCESS (0.027 secs / 0.022 secs) + Chrome 18.0.1025 (Linux): Executed 46 of 46 SUCCESS (0.06 secs / 0.035 secs) + Firefox 28.0.0 (Ubuntu): Executed 46 of 46 SUCCESS (0.037 secs / 0.026 secs) + + ### Inspiring projects - http://www.codeproject.com/Articles/23087/Measurement-Unit-Conversion-Library (a C# / XML based approach) diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..a8155fd --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,97 @@ +// Karma configuration +// Generated on Tue Apr 01 2014 17:31:44 GMT+0200 (CEST) + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: './', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine', 'requirejs'], + + + // list of files / patterns to load in the browser + files: [ + 'test-main.js', + {pattern: '*.js', included: false}, + {pattern: 'test/**/*Spec.js', included: false} + ], + + + // list of files to exclude + exclude: [ + '.git', 'node_modules' + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + // source files, that you wanna generate coverage for + // do not include tests or libraries + // (these files will be instrumented by Istanbul via Ibrik) + 'measurement.js': ['jshint','coverage'] + }, + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['coverage', 'progress'], + + // web server port + port: 9876, + + // enable / disable colors in the output (reporters and logs) + colors: true, + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['PhantomJS'], + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: true, + + // options for karma-jshint + jshint: { + options: { + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + boss: true, + devel: true, + eqnull: true, + browser: true, + globals: { + cordova: true, + jQuery: true + } + }, + summary: true + }, + + coverageReporter: { + dir : './coverage/', + reporters:[ + {type: 'lcov'}, + {type: 'text', file: 'coverage.txt'}, + {type: 'text-summary', file: 'coverage-summary.txt'} + ] + } + }); +}; diff --git a/measurement.js b/measurement.js index 294df91..6869208 100644 --- a/measurement.js +++ b/measurement.js @@ -8,14 +8,13 @@ * @version 0.1 * @example text measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); or measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); * - * @param {Object} namespace + * @param {Object} ns * @returns {undefined} */ -(function(namespace) { +(function(win, ns) { "use strict"; - window.measurement = MeasurementJs; - MeasurementJs.Unit = { + var UNIT = { Speed: { MILES_PER_HOUR: 'mph', KILOMETRE_PER_HOUR: 'km/h', @@ -47,33 +46,31 @@ } }; - var speedUnit = MeasurementJs.Unit.Speed, - pressureUnit = MeasurementJs.Unit.Pressure, - DEFINITIONS = { + var DEFINITIONS = { Speed: { 'mph': { - key: speedUnit.MILES_PER_HOUR, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.MILES_PER_HOUR, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 1.609344 }, 'km/h': { - key: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.KILOMETRE_PER_HOUR, base: null }, 'm/s': { - key: speedUnit.METRE_PER_SECOND, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.METRE_PER_SECOND, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 3.6 }, 'kn': { - key: speedUnit.KNOT, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.KNOT, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 1.852 } }, Distance: { 'km': { - key: MeasurementJs.Unit.Distance.KILOMETRE, + key: UNIT.Distance.KILOMETRE, base: 'm', factor: 1000, name: { @@ -87,7 +84,7 @@ } }, 'm': { - key: MeasurementJs.Unit.Distance.METRE, + key: UNIT.Distance.METRE, base: null, // equals factor of 1 name: { de: 'Meter', @@ -100,19 +97,19 @@ } }, 'mm': { - key: MeasurementJs.Unit.Distance.MILLIMETRE, + key: UNIT.Distance.MILLIMETRE, base: 'm', factor: 0.001 }, 'in': { - key: MeasurementJs.Unit.Distance.INCH, + key: UNIT.Distance.INCH, base: 'm', factor: 0.0254 } }, Pressure: { 'hPa': { - key: pressureUnit.HECTOPASCAL, + key: UNIT.Pressure.HECTOPASCAL, base: 'Pa', factor: 100, name: { @@ -125,7 +122,7 @@ } }, 'Pa': { - key: pressureUnit.PASCAL, + key: UNIT.Pressure.PASCAL, base: null, name: { de: 'Pascal', @@ -137,7 +134,7 @@ } }, 'bar': { - key: pressureUnit.BAR, + key: UNIT.Pressure.BAR, base: 'Pa', factor: 1000000, name: { @@ -152,22 +149,23 @@ }, Temperature: { 'c': { - key: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.CELSIUS, base: null }, 'f': { - key: MeasurementJs.Unit.Temperature.FAHRENHEIT, - base: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.FAHRENHEIT, + base: UNIT.Temperature.CELSIUS, factor: function(value, reverse) { - if (reverse) + if (reverse) { return value * 1.8 + 32; + } return (value - 32) * 5 / 9; } }, 'k': { - key: MeasurementJs.Unit.Temperature.KELVIN, - base: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.KELVIN, + base: UNIT.Temperature.CELSIUS, factor: function(value, reverse) { /** * Really strange rounding error: @@ -176,26 +174,26 @@ * Following workarounds: */ if (reverse) { - return parseFloat((value + 273 + .15).toFixed(10)); + return parseFloat((value + 273 + 0.15).toFixed(10)); } - return (value - 273) - .15; + return (value - 273) - 0.15; } } }, Duration: { 'h': { - key: MeasurementJs.Unit.Duration.HOUR, + key: UNIT.Duration.HOUR, base: 's', factor: 3600 }, 'm': { - key: MeasurementJs.Unit.Duration.MINUTE, + key: UNIT.Duration.MINUTE, base: 's', factor: 60 }, 's': { - key: MeasurementJs.Unit.Duration.SECOND, + key: UNIT.Duration.SECOND, base: null, factor: 1 } @@ -216,13 +214,15 @@ if (inputDef && outputDef) { if (inputDef.base === outputUnit) { - if (typeof inputDef.factor === 'function') + if (typeof inputDef.factor === 'function') { return inputDef.factor(value); + } return value * inputDef.factor; } else if (inputDef.key === outputDef.base) { - if (typeof outputDef.factor === 'function') + if (typeof outputDef.factor === 'function') { return outputDef.factor(value, true); + } return value / outputDef.factor; @@ -234,19 +234,21 @@ * vs. larger configuration array/file size */ var baseType = inputDef.base || outputDef.base, baseValue; - if (typeof baseType === 'undefined') + if (typeof baseType === 'undefined') { return false; + } if (baseType === inputDef.base) { - baseValue = MeasurementJs(unitType).convert(value).from(inputDef.key).to(inputDef.base); + baseValue = mjs(unitType).convert(value).from(inputDef.key).to(inputDef.base); inputUnit = inputDef.base; } else if (baseType === outputDef.base) { - baseValue = MeasurementJs(unitType).convert(value).from(outputDef.key).to(outputDef.base); + baseValue = mjs(unitType).convert(value).from(outputDef.key).to(outputDef.base); inputUnit = outputDef.base; } - if (baseType === MeasurementJs.Unit.Temperature.CELSIUS) + if (baseType === UNIT.Temperature.CELSIUS) { return parseFloat(self.convert(baseValue).toFixed(10)); + } return self.convert(baseValue); } @@ -291,15 +293,17 @@ var easyApiConverter = { from: function(inputUnit) { converter.setInputUnit(inputUnit); - if (readyToConvert()) + if (readyToConvert()) { return converter.convert(valueToConvert); + } return this; }, to: function(outputUnit) { converter.setOutputUnit(outputUnit); - if (readyToConvert()) + if (readyToConvert()) { return converter.convert(valueToConvert); + } return this; } @@ -307,17 +311,30 @@ return easyApiConverter; }; + return { convert: convert }; } - if (typeof namespace !== 'undefined') { - window.MeasurementJs = undefined; - namespace.measurement = MeasurementJs; - namespace.mJs = namespace.MeasurementJs; - namespace.measurement.Converter = MeasurementConverter; - } + var mjs = MeasurementJs; + mjs.Unit = UNIT; + + if (ns === undefined) { + win.measurement = mjs; + } else { + ns.measurement = mjs; + ns.mJs = mjs; + ns.measurement.Converter = MeasurementConverter; + } -})(typeof mJsNamespace !== 'undefined' ? mJsNamespace : undefined); + // AMD definition - i.e. for require.js + if (typeof win.define === "function" && win.define.amd) { + win.define("measurement", [], function() { + return mjs; + }); + } else if (win.module !== undefined && win.module.exports) { + win.module.exports.measurementjs = mjs; + } +})(window, window.mJsNamespace); diff --git a/nbproject/project.properties b/nbproject/project.properties index 916dd65..64edc66 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,14 +1,20 @@ +auxiliary.org-netbeans-modules-css-prep.less_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.less_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.less_2e_mappings=/less:/css +auxiliary.org-netbeans-modules-css-prep.sass_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.sass_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.sass_2e_mappings=/scss:/css +auxiliary.org-netbeans-modules-web-clientproject-api.js_2e_libs_2e_folder=node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/test/unit/compress/expected browser.autorefresh.SL__Browsers_ChromeBrowser=true browser.highlightselection.SL__Browsers_ChromeBrowser=true -config.folder=${file.reference.measurement.js-test} -external.project.url= +config.folder= +file.reference.4theworld-measurementjs=. file.reference.lib-measurement.js=. -file.reference.measurement.js-test=test -file.reference.test-config=test/config +file.reference.measurement.js-test=test/unit +file.reference.test-config=${file.reference.lib-measurement.js} file.reference.test-unit=test/unit files.encoding=UTF-8 -server=INTERNAL site.root.folder=${file.reference.lib-measurement.js} -start.file=index.html +start.file=test/index.html test.folder=${file.reference.test-unit} -web.context.root=/measurement +web.context.root=/measurement/ diff --git a/package.json b/package.json index 11a88cc..bb37622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "measurementjs", - "version": "0.1.0", + "version": "0.1.1-alpha", "description": "Nice unit-of-measure conversion", "readmeFilename": "README.md", "main": "measurement.js", @@ -11,9 +11,19 @@ "license": "MIT", "author": "Philzen ", "scripts": { - "test": "phantomjs test/phantomRunner.js test/index.html" + "karma": "./node_modules/.bin/karma start", + "test": "echo $TRAVIS$CI | grep truetrue>/dev/null && npm run-script travis-publish || npm run-script karma", + "travis-publish": "npm run-script karma && cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "devDependencies": { - "phantomjs": "^1.9.7-1" + "coveralls": "^2.10.0", + "karma": "^0.12.3", + "karma-cli": "^0.0.4", + "karma-coverage": "^0.2.1", + "karma-jasmine": "^0.1.5", + "karma-requirejs": "^0.2.1", + "karma-jshint": ">=0.1", + "karma-phantomjs-launcher": "^0.1.3", + "phantomjs": "<2" } } diff --git a/test-main.js b/test-main.js new file mode 100644 index 0000000..ad7c639 --- /dev/null +++ b/test-main.js @@ -0,0 +1,24 @@ +var allTestFiles = []; +var TEST_REGEXP = /(spec|test)\.js$/i; + +var pathToModule = function(path) { + return path.replace(/^\/base\//, '').replace(/\.js$/, ''); +}; + +Object.keys(window.__karma__.files).forEach(function(file) { + if (TEST_REGEXP.test(file)) { + // Normalize paths to RequireJS module names. + allTestFiles.push(pathToModule(file)); + } +}); + +require.config({ + // Karma serves files under /base, which is the basePath from your config file + baseUrl: '/base', + + // dynamically load all test files + deps: allTestFiles, + + // we have to kickoff jasmine, as it is asynchronous + callback: window.__karma__.start +}); diff --git a/test/browserRunner.js b/test/browserRunner.js new file mode 100644 index 0000000..8f2c4ed --- /dev/null +++ b/test/browserRunner.js @@ -0,0 +1,61 @@ +require.config({ + baseUrl: 'unit', + urlArgs: 'cb=' + Math.random(), + paths: { + jquery: 'lib/jquery', + measurement: '../../measurement', + jasmine: '../lib/jasmine/jasmine', + 'jasmine-html': '../lib/jasmine/jasmine-html' + }, + shim: { + jasmine: { + exports: 'jasmine' + }, + 'jasmine-html': { + deps: [ 'jasmine' ], + exports: 'jasmine' + } + } +}); + +require([ 'jasmine-html' ], function() { + (function() { + var jasmineEnv = jasmine.getEnv(); + jasmineEnv.updateInterval = 250; + var htmlReporter = new jasmine.HtmlReporter(); + jasmineEnv.addReporter(htmlReporter); + + jasmineEnv.specFilter = function( spec ) { + return htmlReporter.specFilter(spec); + }; + var specs = [ + 'mJs.ApiSpec', + 'mJs.convert.DistanceSpec', 'mJs.convert.DurationSpec', + 'mJs.convert.PressureSpec', 'mJs.convert.SpeedSpec', + 'mJs.convert.TemperatureSpec', + 'mJs.CustomNamespaceSpec' + ]; + + function execJasmine( specs ) { + require(specs, function() { + jasmineEnv.execute(); + }); + } + + var currentWindowOnload = window.onload; + function onLoad() { + if (currentWindowOnload) { + currentWindowOnload(); + } + document.querySelector('.version').innerHTML = jasmineEnv.versionString(); + console.log('Executing Tests...'); + execJasmine(specs); + } + + if (document.readyState === "complete") + return onLoad(); + + return window.onload = onLoad; + + })(); +}); diff --git a/test/index.html b/test/index.html index a7f2da0..0c28d26 100644 --- a/test/index.html +++ b/test/index.html @@ -1,58 +1,15 @@ - - - - - - - - - - - - - - - - - - - - - -
-
- + + + + + + + +
+
+ + diff --git a/test/unit/mJs.ApiSpec.js b/test/unit/mJs.ApiSpec.js index 281af7d..96b6bf8 100644 --- a/test/unit/mJs.ApiSpec.js +++ b/test/unit/mJs.ApiSpec.js @@ -1,76 +1,82 @@ 'use strict'; -describe("MeasurementJs API: ", function() { - - it("first call (i.e. `measurementJs('Speed')` can be kept as reference", function() { - expect(typeof measurement('Speed')).toBe('object'); - }); - - it("has a convert() function", function() { - expect(typeof measurement().convert).toBe('function'); - }); - - describe("measurement().convert(someValue)", function() { - var testUnitType = 'Distance', - testValue = 42, - testValue2 = 666, - convertFunction = measurement(testUnitType).convert(testValue), - convertFunction2 = measurement(testUnitType).convert(testValue2), - someUnit = 'km', - someOtherUnit = 'm'; - - it("returns an Object, which", function() { - expect(typeof convertFunction).toBe('object'); - }); - - it("has a from() and a to() function", function() { - expect(convertFunction.from).toBeDefined(); - expect(convertFunction.to).toBeDefined(); - }); - - describe("from()", function() { - it("returns the same object on first chained call", function() { - expect(convertFunction.from()).toBe(convertFunction); - }); - }); - - describe("from(someUnit)", function() { - var fromSomeUnitReturn = convertFunction.from(someUnit); - it("returns the same object on first chained call", function() { - expect(fromSomeUnitReturn).toBe(convertFunction); - }); - - it("returns a numeric value when to(someOtherUnit) is called on the returned object", function() { - // TODO find out why we need to do this again in this scope - BDD doesn't seem scrope safe here - var fromSomeUnitReturn = convertFunction.from(someUnit); - - var returnedOnSecondChainedCall = fromSomeUnitReturn.to(someOtherUnit); - expect(typeof returnedOnSecondChainedCall).toBe('number'); - }); - - it("returns *the same* value when to(someUnit) - the same unit - is called on the returned object", function() { - expect(measurement(testUnitType).convert(testValue).from(someUnit).to(someUnit)).toBe(testValue); - }); - - }); - - - describe("to()", function() { - it("returns the same object on first chained call", function() { - expect(convertFunction2.to()).toBe(convertFunction2); - }); - }); - - - describe("to(someUnit)", function() { - var toSomeUnitReturn = convertFunction2.to(someUnit); - it("returns the same object on first chained call", function() { - expect(toSomeUnitReturn).toBe(convertFunction2); - }); - - it("returns a value when from(someOtherUnit) is called on the returned object", function() { - expect(convertFunction2.to(someUnit).from(someOtherUnit)).toBeDefined(); - }); - }); - }); -}); \ No newline at end of file +define([ 'measurement' ], function( measurement ) { + + describe("MeasurementJs API: ", function() { + + it("first call (i.e. `measurementJs('Speed')` can be kept as reference", function() { + expect(typeof measurement('Speed')).toBe('object'); + }); + + it("has a convert() function", function() { + expect(typeof measurement().convert).toBe('function'); + }); + + describe("measurement().convert(someValue)", function() { + var testUnitType = 'Distance', + testValue = 42, + testValue2 = 666, + convertFunction = measurement(testUnitType).convert(testValue), + convertFunction2 = measurement(testUnitType) + .convert(testValue2), + someUnit = 'km', + someOtherUnit = 'm'; + + it("returns an Object, which", function() { + expect(typeof convertFunction).toBe('object'); + }); + + it("has a from() and a to() function", function() { + expect(convertFunction.from).toBeDefined(); + expect(convertFunction.to).toBeDefined(); + }); + + describe("from()", function() { + it("returns the same object on first chained call", function() { + expect(convertFunction.from()).toBe(convertFunction); + }); + }); + + describe("from(someUnit)", function() { + var fromSomeUnitReturn = convertFunction.from(someUnit); + it("returns the same object on first chained call", function() { + expect(fromSomeUnitReturn).toBe(convertFunction); + }); + + it("returns a numeric value when to(someOtherUnit) is called on the returned object", function() { + // TODO find out why we need to do this again in this scope - BDD doesn't seem scrope safe here + var fromSomeUnitReturn = convertFunction.from(someUnit); + + var returnedOnSecondChainedCall = fromSomeUnitReturn.to(someOtherUnit); + expect(typeof returnedOnSecondChainedCall).toBe('number'); + }); + + it("returns *the same* value when to(someUnit) - the same unit - is called on the returned object", function() { + expect(measurement(testUnitType).convert(testValue) + .from(someUnit).to(someUnit)).toBe(testValue); + }); + + }); + + + describe("to()", function() { + it("returns the same object on first chained call", function() { + expect(convertFunction2.to()).toBe(convertFunction2); + }); + }); + + + describe("to(someUnit)", function() { + var toSomeUnitReturn = convertFunction2.to(someUnit); + it("returns the same object on first chained call", function() { + expect(toSomeUnitReturn).toBe(convertFunction2); + }); + + it("returns a value when from(someOtherUnit) is called on the returned object", function() { + expect(convertFunction2.to(someUnit).from(someOtherUnit)) + .toBeDefined(); + }); + }); + }); + }); +}); diff --git a/test/unit/mJs.CustomNamespaceSpec.js b/test/unit/mJs.CustomNamespaceSpec.js index 24d44e9..cd31e9a 100644 --- a/test/unit/mJs.CustomNamespaceSpec.js +++ b/test/unit/mJs.CustomNamespaceSpec.js @@ -1,7 +1,32 @@ "use strict"; -describe("Assign a custom namespace for MeasurementJs:", function() { - it("just set window.mJsNamespace = YourNamespace before measurement.js include", function(){ - expect(OurCoolCustomNamespace.changeIT.measurement).toBeDefined(); - }); -}); \ No newline at end of file +define([ 'measurement' ], function( measurement ) { + describe("Assign a custom namespace for MeasurementJs:", function() { + +// inject namespace + var scriptNs = document.createElement('script'); + scriptNs.innerHTML = "var OurCoolCustomNamespace = { changeIT: {}};window.mJsNamespace = OurCoolCustomNamespace.changeIT;"; + document.getElementsByTagName('head')[0].appendChild(scriptNs); + console.info('Injected namespace command into page'); + + it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { + var script = document.createElement('script'); + var basePath = '..'; + if (window.__karma__ !== undefined) { + basePath = 'base'; + } + script.src = basePath + "/measurement.js"; + script.async = false; + script.onload = function() { + console.info("CustomNamespaceSpec.js: Dynamically loaded measurement.js with custom namespace option"); + expect(OurCoolCustomNamespace.changeIT.measurement) + .toBeDefined(); + }; + document.getElementsByTagName('head')[0].appendChild(script); + + waitsFor(function() { + return window.OurCoolCustomNamespace.changeIT.measurement !== undefined; + }); + }); + }); +}); diff --git a/test/unit/mJs.convert.Distance.js b/test/unit/mJs.convert.Distance.js deleted file mode 100644 index a8d0710..0000000 --- a/test/unit/mJs.convert.Distance.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -describe("measurement.Unit.Distance", function() { - expect(measurement.Unit.Distance).toBeDefined(); - - /** - * dc Distance Constants - * @type @exp;measurement@pro;Unit@pro;Distance - */ - var dc = measurement.Unit.Distance; - it('is an object', function() { - expect(typeof dc).toBe('object'); - }); - - it('which contains string keys that we can use as constants', function() { - for (var i in dc) { - expect(typeof dc[i]).toBe('string'); - expect(dc[i].length).toBeGreaterThan(0); - } - }); - - describe("convert():", function() { - - it('1 Distance.KILOMETRE equals 1000 Distance.METRE', function(){ - expect(measurement('Distance').convert(1).from(dc.KILOMETRE).to(dc.METRE) ).toBe(1000); - }); - - it('1000 Distance.MILLIMETRES equal 1 Distance.METRES', function(){ - expect(measurement('Distance').convert(1000).from(dc.MILLIMETRE).to(dc.METRE) ).toBe(1); - }); - - it('254 Distance.MILLIMETRES equal 10 Distance.INCH', function(){ - expect(measurement('Distance').convert(254).from(dc.MILLIMETRE).to(dc.INCH) ).toBe(10); - }); - - }); -}); diff --git a/test/unit/mJs.convert.DistanceSpec.js b/test/unit/mJs.convert.DistanceSpec.js new file mode 100644 index 0000000..16625ae --- /dev/null +++ b/test/unit/mJs.convert.DistanceSpec.js @@ -0,0 +1,38 @@ +'use strict'; + +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Distance", function() { + expect(measurement.Unit.Distance).toBeDefined(); + + var dc = measurement.Unit.Distance; + it('is an object', function() { + expect(typeof dc).toBe('object'); + }); + + it('which contains string keys that we can use as constants', function() { + for (var i in dc) { + expect(typeof dc[i]).toBe('string'); + expect(dc[i].length).toBeGreaterThan(0); + } + }); + + describe("convert():", function() { + + it('1 Distance.KILOMETRE equals 1000 Distance.METRE', function() { + expect(measurement('Distance').convert(1).from(dc.KILOMETRE) + .to(dc.METRE)).toBe(1000); + }); + + it('1000 Distance.MILLIMETRES equal 1 Distance.METRES', function() { + expect(measurement('Distance').convert(1000) + .from(dc.MILLIMETRE).to(dc.METRE)).toBe(1); + }); + + it('254 Distance.MILLIMETRES equal 10 Distance.INCH', function() { + expect(measurement('Distance').convert(254).from(dc.MILLIMETRE) + .to(dc.INCH)).toBe(10); + }); + + }); + }); +}); diff --git a/test/unit/mJs.convert.Duration.js b/test/unit/mJs.convert.DurationSpec.js similarity index 95% rename from test/unit/mJs.convert.Duration.js rename to test/unit/mJs.convert.DurationSpec.js index 045d2e9..035bf52 100644 --- a/test/unit/mJs.convert.Duration.js +++ b/test/unit/mJs.convert.DurationSpec.js @@ -1,5 +1,6 @@ "use strict"; +define([ 'measurement' ], function( measurement ) { describe("measurement.Unit.Duration", function() { expect(measurement.Unit.Duration).toBeDefined(); @@ -37,3 +38,4 @@ describe("measurement.Unit.Duration", function() { }); }); +}); \ No newline at end of file diff --git a/test/unit/mJs.convert.Pressure.js b/test/unit/mJs.convert.PressureSpec.js similarity index 94% rename from test/unit/mJs.convert.Pressure.js rename to test/unit/mJs.convert.PressureSpec.js index f0a2a64..3b1b62f 100644 --- a/test/unit/mJs.convert.Pressure.js +++ b/test/unit/mJs.convert.PressureSpec.js @@ -1,5 +1,6 @@ 'use strict'; +define([ 'measurement' ], function( measurement ) { describe("measurement.Unit.Pressure", function() { expect(measurement.Unit.Pressure).toBeDefined(); @@ -31,3 +32,4 @@ describe("measurement.Unit.Pressure", function() { }); }); }); +}); \ No newline at end of file diff --git a/test/unit/mJs.convert.Speed.js b/test/unit/mJs.convert.SpeedSpec.js similarity index 97% rename from test/unit/mJs.convert.Speed.js rename to test/unit/mJs.convert.SpeedSpec.js index 58f6689..62f06ae 100644 --- a/test/unit/mJs.convert.Speed.js +++ b/test/unit/mJs.convert.SpeedSpec.js @@ -1,5 +1,7 @@ "use strict"; +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Speed", function() { expect(measurement.Unit.Speed).toBeDefined(); @@ -67,3 +69,4 @@ describe("measurement.Unit.Speed", function() { }); }); +}); diff --git a/test/unit/mJs.convert.Temperature.js b/test/unit/mJs.convert.TemperatureSpec.js similarity index 97% rename from test/unit/mJs.convert.Temperature.js rename to test/unit/mJs.convert.TemperatureSpec.js index 4796f86..f3691cd 100644 --- a/test/unit/mJs.convert.Temperature.js +++ b/test/unit/mJs.convert.TemperatureSpec.js @@ -1,5 +1,7 @@ 'use strict'; +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Temperature", function() { expect(measurement.Unit.Temperature).toBeDefined(); @@ -66,3 +68,5 @@ describe("measurement.Unit.Temperature", function() { }); }); }); + +}); \ No newline at end of file