Skip to content

Commit 97f7252

Browse files
committed
Added basics for unit testing
1 parent fa2b821 commit 97f7252

6 files changed

+104
-14
lines changed

Gruntfile.coffee

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module.exports = (grunt) ->
2+
3+
# Project configuration.
4+
grunt.initConfig
5+
pkg: grunt.file.readJSON "package.json"
6+
coffeelint:
7+
app: [
8+
"*.coffee"
9+
"accessories/**/*.coffee"
10+
"test/**/*.coffee"
11+
]
12+
options:
13+
no_trailing_whitespace:
14+
level: "ignore"
15+
max_line_length:
16+
value: 100
17+
indentation:
18+
value: 2
19+
level: "error"
20+
no_unnecessary_fat_arrows:
21+
level: 'ignore'
22+
23+
mochaTest:
24+
test:
25+
options:
26+
reporter: "spec"
27+
require: ['coffee-errors'] #needed for right line numbers in errors
28+
src: ["test/*"]
29+
# blanket is used to record coverage
30+
testBlanket:
31+
options:
32+
reporter: "dot"
33+
src: ["test/*"]
34+
coverage:
35+
options:
36+
reporter: "html-cov"
37+
quiet: true
38+
captureFile: "coverage.html"
39+
src: ["test/*"]
40+
41+
grunt.loadNpmTasks "grunt-coffeelint"
42+
grunt.loadNpmTasks "grunt-mocha-test"
43+
44+
grunt.registerTask "blanket", =>
45+
blanket = require "blanket"
46+
47+
blanket(
48+
pattern: (file) ->
49+
if file.match "pimatic-hap/accessories" then return true
50+
#if file.match "pimatic/node_modules" then return false
51+
withoutPrefix = file.replace(/.*\/node_modules\/pimatic/, "")
52+
return (not withoutPrefix.match 'node_modules') and (not withoutPrefix.match "/test/")
53+
loader: "./node-loaders/coffee-script"
54+
)
55+
56+
grunt.registerTask "clean-coverage", =>
57+
fs = require "fs"
58+
path = require "path"
59+
60+
replaceAll = (find, replace, str) =>
61+
escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
62+
str.replace(new RegExp(escapeRegExp(find), 'g'), replace)
63+
64+
file = "#{__dirname}/coverage.html"
65+
html = fs.readFileSync(file).toString()
66+
html = replaceAll path.dirname(__dirname), "", html
67+
fs.writeFileSync file, html
68+
69+
# Default task(s).
70+
grunt.registerTask "default", ["coffeelint", "mochaTest:test"]
71+
grunt.registerTask "test", ["coffeelint", "mochaTest:test"]
72+
grunt.registerTask "coverage",
73+
["blanket", "mochaTest:testBlanket", "mochaTest:coverage", "clean-coverage"]

accessories/base.coffee

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (env) ->
2323
@getService(Service.AccessoryInformation)
2424
.setCharacteristic(Characteristic.Manufacturer, "Pimatic")
2525
.setCharacteristic(Characteristic.Model, "Rev-1")
26-
.setCharacteristic(Characteristic.SerialNumber, serialNumber);
26+
.setCharacteristic(Characteristic.SerialNumber, serialNumber)
2727

2828
@addService(Service.BridgingState)
2929
.getCharacteristic(Characteristic.Reachable)
@@ -69,6 +69,8 @@ module.exports = (env) ->
6969
return false
7070

7171
getServiceOverride: (service) =>
72-
if @hapConfig != null && @hapConfig != undefined && @hapConfig.service != null && @hapConfig.service != undefined && @hapConfig.service of @supportedServiceOverrides
72+
if @hapConfig != null && @hapConfig != undefined &&
73+
@hapConfig.service != null && @hapConfig.service != undefined &&
74+
@hapConfig.service of @supportedServiceOverrides
7375
return @supportedServiceOverrides[@hapConfig.service]
7476
return service

accessories/shutter.coffee

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ module.exports = (env) ->
4343
.setCharacteristic(Characteristic.LockCurrentState, @getLockCurrentState(position))
4444

4545
getLockCurrentState: (position) =>
46-
if position == 'up'
47-
return Characteristic.LockCurrentState.UNSECURED
48-
else if position == "down"
49-
return Characteristic.LockCurrentState.SECURED
50-
else
51-
# stopped somewhere in between
52-
return Characteristic.LockCurrentState.UNKNOWN
46+
if position == 'up'
47+
return Characteristic.LockCurrentState.UNSECURED
48+
else if position == "down"
49+
return Characteristic.LockCurrentState.SECURED
50+
else
51+
# stopped somewhere in between
52+
return Characteristic.LockCurrentState.UNKNOWN

accessories/thermostat.coffee

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ module.exports = (env) ->
2626
.on 'get', (callback) =>
2727
if @_temperature == null
2828
device.getTemperatureSetpoint().then( (temp) =>
29-
@_temperature = temp
30-
callback(null, @_temperature)
31-
)
29+
@_temperature = temp
30+
callback(null, @_temperature)
31+
)
3232
else
3333
callback(null, @_temperature)
3434

@@ -79,7 +79,8 @@ module.exports = (env) ->
7979
device.on 'mode', (mode) =>
8080
if mode == "auto"
8181
@getService(Service.Thermostat)
82-
.setCharacteristic(Characteristic.TargetHeatingCoolingState, Characteristic.TargetHeatingCoolingState.AUTO)
82+
.setCharacteristic(Characteristic.TargetHeatingCoolingState,
83+
Characteristic.TargetHeatingCoolingState.AUTO)
8384

8485
setTemperatureTo: (temp) =>
8586
if @_temperature is temp then return

hap-config-schema.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ module.exports = {
1515
type: "integer"
1616
default: 51826
1717
}
18-
};
18+
}

package.json

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "hap.coffee",
66
"files": [
77
"accessories",
8+
"test",
89
"hap.coffee",
910
"README.md",
1011
"hap-config-schema.coffee",
@@ -18,10 +19,23 @@
1819
"url": "git://github.com/michbeck100/pimatic-hap.git"
1920
},
2021
"configSchema": "hap-config-schema.coffee",
22+
"scripts": {
23+
"test": "grunt test"
24+
},
2125
"dependencies": {
2226
"hap-nodejs": "0.2.5",
2327
"color-convert": "1.0.0"
2428
},
29+
"devDependencies": {
30+
"cassert": "0.1.2",
31+
"grunt-cli": "0.1.11",
32+
"grunt": "0.4.2",
33+
"grunt-coffeelint": "0.0.8",
34+
"grunt-mocha-test": "0.8.1",
35+
"blanket": "1.1.5",
36+
"coffee-errors": "0.8.4",
37+
"mocha": "1.17.0"
38+
},
2539
"peerDependencies": {
2640
"pimatic": "0.8.*"
2741
},

0 commit comments

Comments
 (0)