@@ -12,121 +12,100 @@ import XCTest
1212
1313import Rhino
1414
15- class RhinoWithinContextTests : BaseTest {
16- static var testData : [ [ Any ] ] = [
17- [ " en " , " coffee_maker " , " orderBeverage " , [ " beverage " : " americano " , " numberOfShots " : " double shot " , " size " : " medium " ] ] ,
18- [ " es " , " iluminación_inteligente " , " changeColor " , [ " location " : " habitación " , " color " : " rosado " ] ] ,
19- [ " de " , " beleuchtung " , " changeState " , [ " state " : " aus " ] ] ,
20- [ " fr " , " éclairage_intelligent " , " changeColor " , [ " color " : " violet " ] ] ,
21- [ " it " , " illuminazione " , " spegnereLuce " , [ " luogo " : " bagno " ] ] ,
22- [ " ja " , " sumāto_shōmei " , " 色変更 " , [ " 色 " : " 青 " ] ] ,
23- [ " ko " , " seumateu_jomyeong " , " changeColor " , [ " color " : " 파란색 " ] ] ,
24- [ " pt " , " luz_inteligente " , " ligueLuz " , [ " lugar " : " cozinha " ] ] ]
15+ struct TestData : Decodable {
16+ var tests : TestDataTests
17+ }
2518
26- var language : String = " "
27- var modelPath : String = " "
28- var contextPath : String = " "
29- var testAudioPath : URL ? = URL ( string: " " )
30- var expectedIntent : String = " "
31- var expectedSlots : [ String : String ] = [ : ]
19+ struct TestDataTests : Decodable {
20+ var within_context : [ TestDataWithinContextTest ]
21+ var out_of_context : [ TestDataOutOfContextTest ]
22+ }
3223
33- override class var defaultTestSuite : XCTestSuite {
34- get {
35- let xcTestSuite = XCTestSuite ( name: NSStringFromClass ( self ) )
36- let bundle = Bundle ( for: self )
24+ struct TestDataWithinContextTest : Decodable {
25+ var language : String
26+ var context_name : String
27+ var inference : TestDataInference
28+ }
3729
38- for testCase in testData {
39- let suffix = ( testCase [ 0 ] ) as! String == " en " ? " " : " _ \( testCase [ 0 ] ) "
40- for invocation in testInvocations {
41- let newTestCase = RhinoWithinContextTests ( invocation: invocation)
42- newTestCase. language = testCase [ 0 ] as! String
43- newTestCase. modelPath = bundle. path ( forResource: " rhino_params \( suffix) " , ofType: " pv " ) !
44- newTestCase. contextPath = bundle. path ( forResource: " \( testCase [ 1 ] ) _ios " , ofType: " rhn " ) !
45- newTestCase. testAudioPath = bundle. url ( forResource: " test_within_context \( suffix) " , withExtension: " wav " ) !
46- newTestCase. expectedIntent = testCase [ 2 ] as! String
47- newTestCase. expectedSlots = testCase [ 3 ] as! [ String : String ]
48- xcTestSuite. addTest ( newTestCase)
49- }
50- }
30+ struct TestDataInference : Decodable {
31+ var intent : String
32+ var slots : [ String : String ]
33+ }
5134
52- return xcTestSuite
53- }
54- }
35+ struct TestDataOutOfContextTest : Decodable {
36+ var language : String
37+ var context_name : String
38+ }
5539
40+ class RhinoWithinContextTests : BaseTest {
5641 func testWrapper( ) throws {
57- let inference = try XCTContext . runActivity ( named: " ( \( language) ) " ) { _ -> Inference in
58- let r = try Rhino . init (
59- accessKey: accessKey,
60- contextPath: contextPath,
61- modelPath: modelPath)
62- XCTAssert ( Rhino . version != " " )
63- XCTAssert ( Rhino . frameLength > 0 )
64- XCTAssert ( Rhino . sampleRate > 0 )
65- XCTAssert ( r. contextInfo != " " )
66-
67- let ret = try processFile ( rhino: r, testAudioURL: testAudioPath!)
68- r. delete ( )
69- return ret
42+ let bundle = Bundle ( for: type ( of: self ) )
43+ let testDataJsonUrl = bundle. url ( forResource: " test_data " , withExtension: " json " , subdirectory: " test_resources " ) !
44+
45+ let testDataJsonData = try Data ( contentsOf: testDataJsonUrl)
46+ let testData = try JSONDecoder ( ) . decode ( TestData . self, from: testDataJsonData)
47+
48+ for testCase in testData. tests. within_context {
49+ let suffix = testCase. language == " en " ? " " : " _ \( testCase. language) "
50+
51+ let language : String = testCase. language
52+ let modelPath : String = bundle. path ( forResource: " rhino_params \( suffix) " , ofType: " pv " , inDirectory: " test_resources/model_files " ) !
53+ let contextPath : String = bundle. path ( forResource: " \( testCase. context_name) _ios " , ofType: " rhn " , inDirectory: " test_resources/context_files/ \( testCase. language) " ) !
54+ let testAudioPath : URL = bundle. url ( forResource: " test_within_context \( suffix) " , withExtension: " wav " , subdirectory: " test_resources/audio_samples " ) !
55+ let expectedIntent : String = testCase. inference. intent
56+ let expectedSlots : [ String : String ] = testCase. inference. slots
57+
58+ try XCTContext . runActivity ( named: " ( \( language) ) " ) { _ in
59+ let r = try Rhino . init (
60+ accessKey: accessKey,
61+ contextPath: contextPath,
62+ modelPath: modelPath)
63+ XCTAssert ( Rhino . version != " " )
64+ XCTAssert ( Rhino . frameLength > 0 )
65+ XCTAssert ( Rhino . sampleRate > 0 )
66+ XCTAssert ( r. contextInfo != " " )
67+
68+ let inference = try processFile ( rhino: r, testAudioURL: testAudioPath)
69+ r. delete ( )
70+
71+ XCTAssert ( inference. isUnderstood)
72+ XCTAssert ( inference. intent == expectedIntent)
73+ XCTAssert ( inference. slots == expectedSlots)
74+ }
7075 }
71-
72- XCTAssert ( inference. isUnderstood)
73- XCTAssert ( inference. intent == expectedIntent)
74- XCTAssert ( inference. slots == expectedSlots)
7576 }
7677}
7778
7879class RhinoOutOfContextTests : BaseTest {
79- static var testData : [ [ Any ] ] = [
80- [ " en " , " coffee_maker " ] ,
81- [ " es " , " iluminación_inteligente " ] ,
82- [ " de " , " beleuchtung " ] ,
83- [ " fr " , " éclairage_intelligent " ] ,
84- [ " it " , " illuminazione " ] ,
85- [ " ja " , " sumāto_shōmei " ] ,
86- [ " ko " , " seumateu_jomyeong " ] ,
87- [ " pt " , " luz_inteligente " ] ]
88-
89- var language : String = " "
90- var modelPath : String = " "
91- var contextPath : String = " "
92- var testAudioPath : URL ? = URL ( string: " " )
93-
94- override class var defaultTestSuite : XCTestSuite {
95- get {
96- let xcTestSuite = XCTestSuite ( name: NSStringFromClass ( self ) )
97- let bundle = Bundle ( for: self )
98-
99- for testCase in testData {
100- let suffix = ( testCase [ 0 ] ) as! String == " en " ? " " : " _ \( testCase [ 0 ] ) "
101- for invocation in testInvocations {
102- let newTestCase = RhinoOutOfContextTests ( invocation: invocation)
103- newTestCase. language = testCase [ 0 ] as! String
104- newTestCase. modelPath = bundle. path ( forResource: " rhino_params \( suffix) " , ofType: " pv " ) !
105- newTestCase. contextPath = bundle. path ( forResource: " \( testCase [ 1 ] ) _ios " , ofType: " rhn " ) !
106- newTestCase. testAudioPath = bundle. url ( forResource: " test_out_of_context \( suffix) " , withExtension: " wav " ) !
107- xcTestSuite. addTest ( newTestCase)
108- }
109- }
110-
111- return xcTestSuite
112- }
113- }
114-
11580 func testWrapper( ) throws {
116- let inference = try XCTContext . runActivity ( named: " ( \( language) ) " ) { _ -> Inference in
117- let r = try Rhino . init (
118- accessKey: accessKey,
119- contextPath: contextPath,
120- modelPath: modelPath)
121- XCTAssert ( Rhino . version != " " )
122- XCTAssert ( Rhino . frameLength > 0 )
123- XCTAssert ( Rhino . sampleRate > 0 )
124- XCTAssert ( r. contextInfo != " " )
125- let ret = try processFile ( rhino: r, testAudioURL: testAudioPath!)
126- r. delete ( )
127- return ret
81+ let bundle = Bundle ( for: type ( of: self ) )
82+
83+ let testDataJsonUrl = bundle. url ( forResource: " test_data " , withExtension: " json " , subdirectory: " test_resources " ) !
84+ let testDataJsonData = try Data ( contentsOf: testDataJsonUrl)
85+ let testData = try JSONDecoder ( ) . decode ( TestData . self, from: testDataJsonData)
86+
87+ for testCase in testData. tests. out_of_context {
88+ let suffix = testCase. language == " en " ? " " : " _ \( testCase. language) "
89+
90+ let language : String = testCase. language
91+ let modelPath : String = bundle. path ( forResource: " rhino_params \( suffix) " , ofType: " pv " , inDirectory: " test_resources/model_files " ) !
92+ let contextPath : String = bundle. path ( forResource: " \( testCase. context_name) _ios " , ofType: " rhn " , inDirectory: " test_resources/context_files/ \( testCase. language) " ) !
93+ let testAudioPath : URL = bundle. url ( forResource: " test_out_of_context \( suffix) " , withExtension: " wav " , subdirectory: " test_resources/audio_samples " ) !
94+
95+ try XCTContext . runActivity ( named: " ( \( language) ) " ) { _ in
96+ let r = try Rhino . init (
97+ accessKey: accessKey,
98+ contextPath: contextPath,
99+ modelPath: modelPath)
100+ XCTAssert ( Rhino . version != " " )
101+ XCTAssert ( Rhino . frameLength > 0 )
102+ XCTAssert ( Rhino . sampleRate > 0 )
103+ XCTAssert ( r. contextInfo != " " )
104+ let inference = try processFile ( rhino: r, testAudioURL: testAudioPath)
105+ r. delete ( )
106+
107+ XCTAssert ( !inference. isUnderstood)
108+ }
128109 }
129-
130- XCTAssert ( !inference. isUnderstood)
131110 }
132- }
111+ }
0 commit comments