1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15const test = require("./out/build/Release/napitest") 16const { TestClass1, TestClass2 } = require("./out/build/Release/napitest") 17const { TestClass3, TestClass4 } = require("./out/build/Release/napitest") 18var assert = require("assert"); 19const { type } = require("os"); 20 21describe('Optional basic', function () { 22 23 it('test basic type', function () { 24 let ret = test.fun1("a"); 25 assert.strictEqual(ret, 0); 26 ret = test.fun1("a", "b"); 27 assert.strictEqual(ret, 0); 28 ret = test.fun1("a", "b", 3); 29 assert.strictEqual(ret, 0); 30 ret = test.fun1("a", "b", 3, true); 31 assert.strictEqual(ret, 0); 32 33 let tc = new TestClass1(); 34 ret = tc.interFun1(); 35 assert.strictEqual(ret, 0); 36 ret = tc.interFun1("a"); 37 assert.strictEqual(ret, 0); 38 ret = tc.interFun1("a", "b"); 39 assert.strictEqual(ret, 0); 40 ret = tc.interFun1("a", "b", 3); 41 assert.strictEqual(ret, 0); 42 ret = tc.interFun1("a", "b", 3, true); 43 assert.strictEqual(ret, 0); 44 }); 45}); 46 47describe('Optional Array', function () { 48 49 it('test Array', function () { 50 let ret = test.fun21("a"); 51 assert.strictEqual(ret, 0); 52 ret = test.fun21("a", ['aa', 'bb', 'cc']); 53 assert.strictEqual(ret, 0); 54 ret = test.fun21("a", ['aa', 'bb', 'cc'], [1, 2, 3]); 55 assert.strictEqual(ret, 0); 56 ret = test.fun21("a", 57 ['aa', 'bb', 'cc'], [1, 2, 3], [true, true, true]); 58 assert.strictEqual(ret, 0); 59 }); 60 61 function def(ret) { 62 assert.deepStrictEqual(ret, ''); 63 } 64 65 it('test AsyncCallback<Array>', function () { 66 test.fun23('15').then(def); 67 }); 68 69 it('test AsyncCallback<Array>', function () { 70 let promiseObj = test.fun23('15'); 71 promiseObj.then(ret => { def(ret) }); 72 }); 73 74 it('test interface array', function () { 75 let tc = new TestClass2(); 76 ret = tc.interFun21(); 77 assert.strictEqual(ret, 0); 78 ret = tc.interFun21([1, 2, 3, 4]); 79 assert.strictEqual(ret, 0); 80 ret = tc.interFun21([1, 2, 3, 4], ['a', 'b', 'c', 'd']); 81 assert.strictEqual(ret, 0); 82 ret = tc.interFun21([1, 2, 3, 4], 83 ['a', 'b', 'c', 'd'], [true, false, true, false]); 84 assert.strictEqual(ret, 0); 85 }); 86}); 87 88describe('Optional Map', function () { 89 90 it('test map{}', function () { 91 let ret = test.fun31("a"); 92 assert.strictEqual(ret, 0); 93 ret = test.fun31("a", { 'test': 15, 'test1': 18 }); 94 assert.strictEqual(ret, 0); 95 }); 96 97 it('test map<>', function () { 98 let ret = test.fun32("a"); 99 assert.strictEqual(ret, 0); 100 ret = test.fun32("a", { 'test': '15', 'test1': '18' }); 101 assert.strictEqual(ret, 0); 102 }); 103 104 it('test interface map{}', function () { 105 let tc = new TestClass3(); 106 ret = tc.interFun31('aaaa'); 107 assert.strictEqual(ret, 0); 108 ret = tc.interFun31('aaaa', { 'test': 18, 'tst1': 20 }); 109 assert.strictEqual(ret, 0); 110 }); 111 112 it('test interface map<>', function () { 113 let tc = new TestClass3(); 114 ret = tc.interFun32('aaaa'); 115 assert.strictEqual(ret, 0); 116 ret = tc.interFun32('aaaa', { 'test': true, 'tst1': false }); 117 assert.strictEqual(ret, 0); 118 }); 119}); 120 121describe('Optional enum', function () { 122 var GrantStatus = { 123 PERMISSION_DEFAULT: "", 124 PERMISSION_DENIED: "-1", 125 PERMISSION_GRANTED: "2", 126 PERMISSION_PASS: "3", 127 } 128 var HttpStatus = { 129 STATUS0: 0, 130 STATUS1: 500, 131 STATUS2: 503, 132 } 133 function cb3(ret) { 134 assert.strictEqual(typeof ret, 'number'); 135 } 136 it('test enum type', function () { 137 let ret = test.fun41(); 138 assert.strictEqual(ret, 0); 139 ret = test.fun41(HttpStatus.STATUS1); 140 assert.strictEqual(ret, 0); 141 ret = test.fun41(HttpStatus.STATUS1, GrantStatus.PERMISSION_DENIED); 142 assert.strictEqual(ret, 0); 143 ret = test.fun42('1'); 144 ret = test.fun42('1', cb3); 145 }); 146}); 147 148describe('Optional interface', function () { 149 function cb4(ret) { 150 assert.notEqual(ret.name, undefined) 151 assert.notEqual(ret.age, undefined) 152 } 153 it('test interface', function () { 154 let ret = test.fun51(); 155 assert.strictEqual(ret, 0); 156 ret = test.fun51({ name: 'n1', age: 20 }); 157 assert.strictEqual(ret, 0); 158 ret = test.fun51({ name: 'n1', age: 20 }, { name: 'n2', age: 30 }); 159 assert.strictEqual(ret, 0); 160 ret = test.fun51({ name: 'n1', age: 20 }, 161 { name: 'n2', age: 30 }, { name: 'n3', age: 40 }); 162 assert.strictEqual(ret, 0); 163 164 ret = test.fun52({ name: 'n1', age: 20 }); 165 assert.strictEqual(ret, 0); 166 ret = test.fun52({ name: 'n1', age: 20 }, 167 [{ name: 'm1', age: 121 }, { name: 'm2', age: 123 }]); 168 assert.strictEqual(ret, 0); 169 170 ret = test.fun53({ name: 'n1', age: 20 }); 171 ret = test.fun53({ name: 'n1', age: 20 }, cb4); 172 173 ret = test.fun61(); 174 assert.strictEqual(ret, 0); 175 ret = test.fun61({ name: 'n2', age: 25 }); 176 assert.strictEqual(ret, 0); 177 }); 178 179 it('test interface type', function () { 180 let tc = new TestClass4(); 181 ret = tc.interFun51({ name: 'n2', age: 25 }); 182 assert.strictEqual(ret, 0); 183 ret = tc.interFun51({ name: 'n2', age: 25 }, { name: 'n2', age: 25 }); 184 assert.strictEqual(ret, 0); 185 ret = tc.interFun51({ name: 'n2', age: 25 }, 186 { name: 'n2', age: 25 }, { name: 'n2', age: 25 }); 187 assert.strictEqual(ret, 0); 188 }); 189 190}); 191 192