Skip to content

Commit

Permalink
Add more tests for the device factory utils module
Browse files Browse the repository at this point in the history
So code coverage doesn't go down too much
  • Loading branch information
gcampax committed Oct 18, 2019
1 parent 1337835 commit 3b03cec
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -16,6 +16,7 @@ async function seq(array) {
seq([
('./test_unit'),
('./test_device_factories'),
('./test_discovery_services'),
('./test_string_format'),
('./test_version'),
('./test_class'),
Expand Down
30 changes: 30 additions & 0 deletions test/test_device_factories.js
Expand Up @@ -15,6 +15,19 @@ const ThingTalk = require('thingtalk');
const DeviceFactoryUtils = require('../lib/device_factory_utils');

const TEST_CASES = [
[`abstract class @security-camera {}`, {
name: 'Security Camera',
category: 'physical',
}, null],

[`class @org.thingpedia.builtin.thingengine.builtin {
import loader from @org.thingpedia.builtin();
import config from @org.thingpedia.config.builtin();
}`, {
name: 'Security Camera',
category: 'physical',
}, null],

[`class @com.bing {
import loader from @org.thingpedia.v2();
import config from @org.thingpedia.config.none();
Expand Down Expand Up @@ -46,6 +59,23 @@ const TEST_CASES = [
]
}],

[`class @com.bodytrace.scale2 {
import loader from @org.thingpedia.v2();
import config from @org.thingpedia.config.basic_auth();
}`, {
name: "BodyTrace Scale",
category: 'physical',
}, {
type: 'form',
text: "BodyTrace Scale",
kind: 'com.bodytrace.scale2',
category: 'physical',
fields: [
{ name: 'username', label: 'Username', type: 'text' },
{ name: 'password', label: 'Password', type: 'password' }
]
}],

[`class @org.thingpedia.rss {
import loader from @org.thingpedia.rss();
import config from @org.thingpedia.config.form(params=makeArgMap(url : Entity(tt:url)));
Expand Down
63 changes: 63 additions & 0 deletions test/test_discovery_services.js
@@ -0,0 +1,63 @@
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond Cloud
//
// Copyright 2018 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";

const assert = require('assert');
const ThingTalk = require('thingtalk');

const DeviceFactoryUtils = require('../lib/device_factory_utils');

const TEST_CASES = [
[`abstract class @security-camera {}`, []],

[`class @com.bing {
import loader from @org.thingpedia.v2();
import config from @org.thingpedia.config.none();
}`, []],

[`class @com.lg.tv.webos2 {
import loader from @org.thingpedia.v2();
import config from @org.thingpedia.config.discovery.upnp(search_target=['urn:lge:com:service:webos:second-screen-1']);
}`, [{
discovery_type: 'upnp',
service: 'lge-com-service-webos-second-screen-1'
}]],

[`class @org.thingpedia.bluetooth.speaker.a2dp {
import loader from @org.thingpedia.v2();
import config from @org.thingpedia.config.discovery.bluetooth(uuids=['0000110b-0000-1000-8000-00805f9b34fb']);
}`, [{
discovery_type: 'bluetooth',
service: 'uuid-0000110b-0000-1000-8000-00805f9b34fb'
}]],
];

async function testCase(i) {
console.log(`Test Case #${i+1}`);
const [classCode, expectedServices] = TEST_CASES[i];

const classDef = ThingTalk.Grammar.parse(classCode).classes[0];
const generatedServices = DeviceFactoryUtils.getDiscoveryServices(classDef);

try {
assert.deepStrictEqual(generatedServices, expectedServices);
} catch(e) {
console.error('Failed: ' + e.message);
if (process.env.TEST_MODE)
throw e;
}
}
async function main() {
for (let i = 0; i < TEST_CASES.length; i++)
await testCase(i);
}
module.exports = main;
if (!module.parent)
main();

0 comments on commit 3b03cec

Please sign in to comment.