Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
d4083f26
H
Hello UTS
项目概览
DCloud
/
Hello UTS
通知
1598
Star
27
Fork
9
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
Hello UTS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
d4083f26
编写于
8月 22, 2024
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'dev' of gitcode.net:dcloud/hello-uts into dev
上级
80894ed0
807c25fa
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
460 addition
and
7 deletion
+460
-7
pages/SyntaxCase/index.uvue
pages/SyntaxCase/index.uvue
+15
-2
uni_modules/uts-syntaxcase/utssdk/index.uts
uni_modules/uts-syntaxcase/utssdk/index.uts
+101
-0
uni_modules/uts-tests/utssdk/Promise.uts
uni_modules/uts-tests/utssdk/Promise.uts
+332
-0
uni_modules/uts-tests/utssdk/index.uts
uni_modules/uts-tests/utssdk/index.uts
+12
-5
未找到文件。
pages/SyntaxCase/index.uvue
浏览文件 @
d4083f26
...
...
@@ -54,6 +54,9 @@
<button @click="testUtsClassSetter">点击测试class 示例setter方法</button>
<view>测试setter:{{ format(testUtsClassSetterResult) }}</view>
<button @click="testAll">点击测试所有</button>
<!-- #ifdef APP-IOS -->
<button @click="testPromiseStaticMethod">点击测试promise静态方法</button>
<!-- #endif -->
</view>
<view style="height: 20px;"></view>
</scroll-view>
...
...
@@ -74,6 +77,11 @@
SetterTest,
} from "../../uni_modules/uts-syntaxcase";
// #endif
// #ifdef APP-IOS
import {
testPromiseStaticMethod
} from "@/uni_modules/uts-syntaxcase"
// #endif
let test:Test|null = null
let id = 0
export default {
...
...
@@ -442,6 +450,11 @@
console.error("testUtsClassSetter", e);
}
},
// #ifdef APP-IOS
testPromiseStaticMethod() {
testPromiseStaticMethod()
}
// #endif
}
}
</script>
\ No newline at end of file
uni_modules/uts-syntaxcase/utssdk/index.uts
浏览文件 @
d4083f26
import
{
RequestTask
,
SyncOptions
,
AsyncOptions
,
SyntaxResult
,
SyncResult
,
TestOptions
}
from
"./interface.uts"
;
import
{
log
}
from
"./utils.uts"
;
// #ifdef APP-IOS
import
{
UTSPromiseAggregateError
,
UTSPromiseFulfilledResult
,
UTSPromiseRejectedResult
}
from
"DCloudUTSFoundation"
;
// #endif
/**
* 导出一个属性
...
...
@@ -121,6 +124,104 @@ export class Test {
}
}
// #ifdef APP-IOS
// #ifdef UNI-APP-X
export
function
testPromiseStaticMethod
()
{
let
p0
=
new
Promise
<
SyntaxResult
|
null
>
((
resolve
,
reject
)
=>
{
let
success
=
true
setTimeout
(()
=>
{
if
(
success
)
{
const
res
:
SyntaxResult
=
{
name
:
"pomise 0"
,
}
resolve
(
res
);
}
else
{
reject
(
new
Error
(
"this is promise 0 reject reasion"
))
}
},
1000
);
});
let
p1
=
new
Promise
<
SyntaxResult
|
null
>
((
resolve
,
reject
)
=>
{
let
success
=
true
setTimeout
(()
=>
{
if
(
success
)
{
resolve
(
null
);
}
else
{
reject
(
new
Error
(
"this is promise 1 reject reasion"
))
}
},
2000
);
});
let
p2
=
new
Promise
<
SyntaxResult
|
null
>
((
resolve
,
reject
)
=>
{
let
success
=
false
setTimeout
(()
=>
{
if
(
success
)
{
resolve
(
null
);
}
else
{
// reject(new Error("this is promise 2 reject reasion"))
reject
(
null
)
}
},
2500
);
});
// test for any
let
pt
=
Promise
.
any
([
p0
,
p1
,
p2
]);
pt
.
then
(
(
res
)
=>
{
console
.
log
(
"promise.any test success"
,
res
)
})
.
catch
(
(
error
:
any
|
null
)
=>
{
if
(
error
instanceof
UTSPromiseAggregateError
)
{
let
err
=
error
as
UTSPromiseAggregateError
console
.
log
(
err
.
name
,
err
.
message
,
err
.
errors
);
}
else
{
console
.
log
(
error
);
}
})
// test for allSettled
Promise
.
allSettled
([
p0
,
p1
,
p2
])
.
then
((
res
)
=>
{
res
.
forEach
((
item
,
index
)
=>
{
if
(
item
instanceof
UTSPromiseFulfilledResult
<
SyntaxResult
|
null
>
)
{
let
item0
=
item
as
UTSPromiseFulfilledResult
console
.
log
(
item0
.
value
,
"UTSPromiseFulfilledResult value"
)
}
else
if
(
item
instanceof
UTSPromiseRejectedResult
<
SyntaxResult
|
null
>
)
{
let
item0
=
item
as
UTSPromiseRejectedResult
console
.
log
(
item0
.
reason
,
"UTSPromiseRejectedResult reason"
)
}
})
})
.
catch
((
error
:
any
|
null
)
=>
{
console
.
log
(
error
,
"rejected test for promise.allSettled"
)
})
// test for all
Promise
.
all
([
p0
,
p1
,
p2
])
.
then
((
res
)
=>
{
console
.
log
(
res
,
res
.
count
)
})
.
catch
(
(
error
:
any
|
null
)
=>
{
console
.
log
(
error
,
"rejected test for promise.all"
)
})
// test for race
Promise
.
race
([
p0
,
p1
,
p2
])
.
then
((
res
)
=>
{
console
.
log
(
res
,
"resolved test for promise.race"
);
})
.
catch
((
error
:
any
|
null
)
=>
{
console
.
log
(
error
,
"catch test for promise.race"
)
})
}
// #endif
// #endif
export
class
SetterTest
{
private
_nickName
:
string
=
''
;
...
...
uni_modules/uts-tests/utssdk/Promise.uts
0 → 100644
浏览文件 @
d4083f26
import { describe, test, expect, Result } from './tests.uts'
export function testPromise(): Result {
return describe("Promise", () => {
// #ifdef APP-IOS
test('constructor', () => {
// 常规方式
let p = new Promise<Int>((resolve, reject) => {
let success = true;
setTimeout(()=>{
if (success) {
resolve(2024)
} else{
let error = new Error("something going wrong")
reject(error)
}
}, 10);
});
p.then((res: Int) => {
expect(res).toEqual(2024)
})
// 单个函数
let p1 = new Promise<any | null>((resolve: (value: any | null) => void) => {
setTimeout(function() {
resolve(null)
}, 10);
})
p1.then((res: any | null) => {
expect(res).toEqual(null)
})
// 指定函数类型
let p2 = new Promise((resolve: (value: string) => void, reject: (error: any | null) => void) => {
setTimeout(function() {
reject(null)
}, 10);
})
p2.then()
.catch((error: any | null) => {
expect(error).toEqual(null)
})
})
test('then', () => {
let p = new Promise<string>((reslove, reject) => {
setTimeout(function() {
reslove("hello world")
}, 13);
})
p.then(
(res: string): Promise<string> => {
expect(res).toEqual("hello world")
let p0 = new Promise<string>((reslove,reject) => {
var success = true;
setTimeout(function() {
success = true;
if (success) {
reslove(res + " 2024")
} else{
reject("first then p0 reject message")
}
}, 8);
})
return p0;
},
(err: any | null): void => {
throw new Error("first then throw error")
}
)
.catch(
(err):string => {
return "first catch return message"
}
)
.then(
(res) => {
console.log(res)
expect(res).toEqual("hello world 2024")
}
)
.catch(
(err) => {
console.log(err, "this is seconded catch")
}
)
.finally(() => {
console.log("finally")
expect("finally").toEqual("finally")
})
})
test('catch', () => {
})
test('finally', () => {
Promise.resolve(2)
.finally(() => {
console.log("finally")
})
.then((res) => {
expect(res).toEqual(2)
})
Promise.reject(3)
.finally(() => {
console.log("finally")
})
.catch((res) => {
expect(res).toEqual(3)
})
})
test('resolve', () => {
// resolve array
Promise.resolve([1,2,3])
.then((res) => {
expect(res).toEqual([1,2,3])
})
// resolve string
Promise.resolve("成功")
.then((res) => {
expect(res).toEqual("成功");
})
// resolve null
Promise.resolve()
.then((res) => {
// expect(res).toEqual(null);
})
// resolve promise
const original = Promise.resolve(33)
const cast = Promise.resolve(original)
cast.then((res) => {
expect(res).toEqual(33);
})
// 注:此处飘红是编译器误报,实际可以判等
expect(original === cast).toEqual(true)
})
test('reject', () => {
// reject array
Promise.reject([1,2,3])
.catch((err) => {
expect(err).toEqual([1,2,3])
})
// reject error
Promise.reject(new Error("this is reject error message"))
.catch((err) => {
expect((err as Error).message).toEqual("this is reject error message");
})
// reject null
Promise.reject()
.catch((err) => {
expect(err).toEqual(null);
})
// reject promise
const original = Promise.reject(33)
const cast = Promise.reject(original)
cast.catch((err) => {
expect((err as Promise<any>) === original).toEqual(true);
})
expect(original === cast).toEqual(false)
})
test('all', () => {
let p0 = new Promise<string>((resolve, reject) => {
setTimeout(function() {
resolve("1");
}, 10);
})
let p1 = new Promise<string>((resolve, reject) => {
setTimeout(function() {
resolve("2");
}, 10);
})
let p2 = new Promise<string>((resolve, reject) => {
setTimeout(function() {
resolve("3");
}, 10);
})
let p3 = new Promise<string>((resolve, reject) => {
setTimeout(function() {
let error = new Error("p3 reject reason")
reject(error);
}, 10);
})
// resolve
Promise.all([p0, p1, p2])
.then((res) => {
console.log(res);
expect(res).toEqual(["1", "2", "3"])
})
// reject
Promise.all([p1, p2, p3])
.then()
.catch((error) => {
console.log(error);
expect((error as Error).message).toEqual("p3 reject reason");
})
})
test('allSettled', () => {
let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve("1");
}, 10);
})
let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve(null);
}, 10);
})
let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
reject(null)
}, 10);
})
let p3 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
let error = new Error("p3 reject reason")
reject(error);
}, 10);
})
Promise.allSettled([p0, p1, p2, p3])
.then((res) => {
console.log(res);
let statusArr : string[] = []
res.forEach((item, index) => {
statusArr.append(item.status)
})
expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"])
})
})
test('any', () => {
let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve("1");
}, 10);
})
let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve(null);
}, 15);
})
let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
reject(null)
}, 10);
})
let p3 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
reject("error");
}, 12);
})
// test for resolve
Promise.any([p0, p1, p2])
.then((res) => {
console.log(res)
expect(res).toEqual("1");
})
// test for reject
Promise.any([p2, p3])
.then()
.catch(
(error: any | null) => {
console.log("test for promise.any error ==> ", error);
expect((error as UTSPromiseAggregateError).name).toEqual("AggregateError");
expect((error as UTSPromiseAggregateError).message).toEqual("All promises were rejected");
expect((error as UTSPromiseAggregateError).errors).toEqual([null, "error"]);
})
})
test('race', () => {
let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve("fast");
}, 10);
})
let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
resolve("slow");
}, 15);
})
let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() {
reject(null)
}, 12);
})
// test for resolve
Promise.race([p0, p1])
.then((res) => {
console.log("test for race resolve ===> ", res);
expect(res).toEqual("fast");
})
// test for reject
Promise.race([p1, p2])
.then()
.catch((error) => {
console.log("test for race reject ===> ", error);
expect(error).toEqual(null);
})
})
// #endif
})
}
uni_modules/uts-tests/utssdk/index.uts
浏览文件 @
d4083f26
...
...
@@ -19,9 +19,9 @@ import { testType } from './Type.uts'
export { Result } from './tests.uts'
import { testArrayBuffer } from './ArrayBuffer.uts'
import { testNativeCode } from './NativeCode.uts'
import { testPromise} from "./Promise.uts"
// Promise、Proxy、Reflect、Weakmap、WeakSet 不支持
// Proxy、Reflect、Weakmap、WeakSet 不支持
export function runTests() : UTSJSONObject {
const ArrayRes = testArray();
const DateRes = testDate();
...
...
@@ -46,6 +46,10 @@ export function runTests() : UTSJSONObject {
const ArrayBufferRes = testArrayBuffer();
// #endif
const NativeCodeRes = testNativeCode();
// #ifdef APP-IOS
const PromiseRes = testPromise();
// #endif
return {
Array: ArrayRes,
Date: DateRes,
...
...
@@ -68,6 +72,9 @@ export function runTests() : UTSJSONObject {
// #ifdef APP-ANDROID || WEB
ArrayBuffer: ArrayBufferRes,
// #endif
NativeCode: NativeCodeRes
NativeCode: NativeCodeRes,
// #ifdef APP-IOS
Primise: PromiseRes
// #endif
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录