# Hamcrest **Repository Path**: wangyingjun01/Hamcrest ## Basic Information - **Project Name**: Hamcrest - **Description**: No description available - **Primary Language**: Unknown - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 13 - **Created**: 2024-09-14 - **Last Updated**: 2025-02-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # hamcrest ## 简介 > hamcrest是匹配器库,可以组合起来匹配。 ![operation.png](screenshots/operation.png) ## 下载安装 ```shell ohpm install @ohos/hamcrest ``` OpenHarmony ohpm环境配置等更多内容,请参考 [如何安装OpenHarmony ohpm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) 。 ## 使用说明 1. 引入文件及代码依赖 ``` import {AllOf} from '@ohos/hamcrest' ``` 2. 构建匹配器 ``` let matcher = AllOf.allOfMatches(StringContains.containsString('expected'), StringContains.containsString('value')) ``` 3. 调用匹配器匹配 ``` hasProperty() { let matcher = HasProperty.hasProperty('name') console.info(TAG + "hasProperty: " + matcher.matches({ name: 'Joe' })) console.info(TAG + "hasProperty: " + matcher.matches({ name: 'Joel' })) console.info(TAG + "hasProperty: " + matcher.matches({ age: 19 })) } hasLength() { let matcher = CharSequenceLength.hasLength(4); console.info(TAG + "hasLength: " + matcher.matches('aaaa')) console.info(TAG + "hasLength: " + matcher.matches('aaa')) console.info(TAG + "hasLength: " + matcher.matches('aa a')) } anyOf() { let matcherAnyOf = AnyOf.anyOfMatches(IsBlankString.blankString(), IsEqualIgnoringCase.equalToIgnoringCase('aaa')) console.log(TAG + 'anyOf: enter matcherAnyOf:' + matcherAnyOf.matches('AAA')) console.log(TAG + 'anyOf: enter matcherAnyOf:' + matcherAnyOf.matches('bbb')) console.log(TAG + 'anyOf: enter matcherAnyOf:' + matcherAnyOf.matches('')) console.log(TAG + 'anyOf: enter matcherAnyOf:' + matcherAnyOf.matches('\n\r\t')) } containsString() { let matcher = StringContains.containsString('rat') console.info(TAG + "containsString: " + matcher.matches('jim the rat')) console.info(TAG + "containsString: " + matcher.matches('jim the rats')) let matcher2 = StringContains.containsString('rats'); console.info(TAG + "containsString: " + matcher2.matches('jim the rat')) let matcher3 = StringContains.containsStringIgnoringCase('Jim') console.info(TAG + "containsStringIgnoringCase: " + matcher3.matches('jim the rat')) } allOf() { let matcher = AllOf.allOfMatches(StringContains.containsString('expected'), StringContains.containsString('value')) console.info(TAG + "allOf: " + matcher.matches('expected value')) console.info(TAG + "allOf: " + matcher.matches('value expected')) console.info(TAG + "allOf: " + matcher.matches('expected valu')) } endsWith() { let matcher = StringEndsWith.endsWith('test') console.info(TAG + "endsWith: " + matcher.matches('aaabbbtest')) console.info(TAG + "endsWith: " + matcher.matches('aaabbbtests')) console.info(TAG + "endsWith: " + matcher.matches('testdjsdojw')) let matcher2 = StringEndsWith.endsWithIgnoringCase('Test') console.info(TAG + "endsWithIgnoringCase: " + matcher2.matches('this test')) } isNaN() { let matcher = IsNaN.notANumber() console.info(TAG + "isNaN: " + matcher.matches(7)) console.info(TAG + "isNaN: " + matcher.matches(7.7)) console.info(TAG + "isNaN: " + matcher.matches(Number.NaN)) } isEqual() { const value = [1, 2] let matcher = IsEqual.equalTo(value) console.info(TAG + "isEqual: " + matcher.matches([1, 3])) console.info(TAG + "isEqual: " + matcher.matches([1, 2])) let matcher2 = IsEqual.equalTo("string value") console.info(TAG + "isEqual: " + matcher2.matches("string value")) console.info(TAG + "isEqual: " + matcher2.matches("string values")) } equalIgnoringCase() { let matcher = IsEqualIgnoringCase.equalToIgnoringCase('Hello') console.log(TAG + 'equalIgnoringCase:' + matcher.matches('heLLo')) console.log(TAG + 'equalIgnoringCase:' + matcher.matches('hello')) console.log(TAG + 'equalIgnoringCase:' + matcher.matches('HELLO')) } orderingComparison() { let matcher = OrderingComparison.greaterThan(5) console.log(TAG + 'orderingComparison:' + matcher.matches('6')) console.log(TAG + 'orderingComparison:' + matcher.matches('4')) console.log(TAG + 'orderingComparison:' + matcher.matches('5')) let matcher2 = OrderingComparison.greaterThanOrEqualTo(5) console.log(TAG + 'orderingComparison:' + matcher2.matches('6')) console.log(TAG + 'orderingComparison:' + matcher2.matches('4')) console.log(TAG + 'orderingComparison:' + matcher2.matches('5')) let matcher3 = OrderingComparison.lessThan(5) console.log(TAG + 'orderingComparison:' + matcher3.matches('6')) console.log(TAG + 'orderingComparison:' + matcher3.matches('4')) console.log(TAG + 'orderingComparison:' + matcher3.matches('5')) let matcher4 = OrderingComparison.lessThanOrEqualTo(5) console.log(TAG + 'orderingComparison:' + matcher4.matches('6')) console.log(TAG + 'orderingComparison:' + matcher4.matches('4')) console.log(TAG + 'orderingComparison:' + matcher4.matches('5')) let matcher5 = OrderingComparison.comparesEqualTo(5) console.log(TAG + 'orderingComparison:' + matcher5.matches(5.00)) console.log(TAG + 'orderingComparison:' + matcher5.matches(5.1)) console.log(TAG + 'orderingComparison:' + matcher5.matches(0.0)) } isBlackString() { let matcher = IsBlankString.blankString(); console.log(TAG + 'isBlackString :' + matcher.matches('')) console.log(TAG + 'isBlackString :' + matcher.matches('\n\r\t')) let matcher2 = IsBlankString.blankOrNullString(); console.log(TAG + 'isBlackString: enter matcher2:' + matcher2.matches('\n\r\t')) console.log(TAG + 'isBlackString: enter matcher2:' + matcher2.matches('null')) console.log(TAG + 'isBlackString: enter matcher2:' + matcher2.matches(null)) } isCloseTo() { let matcher = IsCloseTo.closeTo(1.1, 0.0); console.log(TAG + 'IsCloseTo :' + matcher.matches(1.10)) let matcher2 = IsCloseTo.closeTo(1.111111111, 0.0); console.log(TAG + 'IsCloseTo :' + matcher2.matches(1.1111111112)) } isNull() { let matcher = IsNull.nullValue() console.log(TAG + 'isNull :' + matcher.matches(null)) console.log(TAG + 'isNull :' + matcher.matches('null')) console.log(TAG + 'isNull :' + matcher.matches('')) } combinationMatch() { let matcher = IsNot.not(StringContains.containsString('expected')); console.info(TAG + "combinationMatch: " + matcher.matches("expected value")) console.info(TAG + "combinationMatch: " + matcher.matches('another value')) let match = Is.is(StringStartsWith.startsWith('hamjest')) console.info(TAG + "combinationMatch: " + match.matches('hamjest is awesome')) } isArray() { let matcher: Matcher = IsArray.array(IsEqual.equalTo('a'), IsEqual.equalTo('b'), IsEqual.equalTo('c')) console.info(TAG + "isArray: " + matcher.matches(['a', 'b', 'c'])) console.info(TAG + "isArray: " + matcher.matches(['a', 'd', 'b'])) console.info(TAG + "isArray: " + matcher.matches(['a', 'b'])) } isIn() { let matcher: Matcher = IsIn.isIn(["bar", "foo"]) console.info(TAG + "isIn: " + matcher.matches("bar")) console.info(TAG + "isIn: " + matcher.matches(["bar", "foo"])) } every() { let matcher: Matcher> = Every.everyItem(StringContains.containsString('a')) console.info(TAG + "every: " + matcher.matches(["AaA", "BaB", "CaC"])) console.info(TAG + "every: " + matcher.matches(["AAA", "BaB", "CbC"])) } matchesPattern() { let matcher = MatchesPattern.matchesPattern(new RegExp('bb')) console.info(TAG + "matchesPattern: " + matcher.matches('aabbcc')) console.info(TAG + "matchesPattern: " + matcher.matches('aabcc')) console.info(TAG + "matchesPattern: " + matcher.matches('abc')) let matcher2 = MatchesPattern.matchesStringPattern("^Hello.*$") console.info(TAG + "matchesPattern: " + matcher2.matches('Hello, world!')) console.info(TAG + "matchesPattern: " + matcher2.matches('ello, world!')) } isEqualCompressingWhiteSpace() { let matcher = IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace('hamjest') console.info(TAG + "isEqualCompressingWhiteSpace: " + matcher.matches(' hamjest')) console.info(TAG + "isEqualCompressingWhiteSpace: " + matcher.matches(' ham jest')) console.info(TAG + "isEqualCompressingWhiteSpace: " + matcher.matches('Ham jest')) } isSame() { let arr: Array = new Array(1) arr[0] = "hello" let arr1: Array = new Array(1) arr1[0] = "hello" let matcher = IsSame.sameInstance(arr) console.info(TAG + "isSame sameInstance: " + matcher.matches(arr)) console.info(TAG + "isSame: sameInstance" + matcher.matches(arr1)) let str: string = "hello" let matcher1 = IsSame.theInstance(str) console.info(TAG + "isSame: theInstance:" + matcher1.matches(str)) let matcher2 = IsSame.theInstance(arr) console.info(TAG + "isSame: theInstance:" + matcher2.matches(arr1)) } isIterableContaining() { let map: Map = new Map() map.set("name", "张三") let matcher = IsIterableContaining.hasItem(StringContains.containsString('张三')) console.info(TAG + "isIterableContaining hasItem : " + matcher.matches(map)) let matcher1 = IsIterableContaining.hasItem(StringContains.containsString('王')) console.info(TAG + "isIterableContaining hasItem : " + matcher1.matches(map)) } isEmptyString() { let matcher :Matcher= IsEmptyString.emptyString() let test:string="" console.info(TAG + "isEmptyString : " + matcher.matches(test)) let test2:string="hello" console.info(TAG + "isEmptyString : " + matcher.matches(test2)) let matcher2 = IsEmptyString.emptyOrNullString() console.info(TAG + "isEmptyString : " + matcher2.matches("")) console.info(TAG + "isEmptyString : " + matcher2.matches("z")) console.info(TAG + "isEmptyString : " + matcher2.matches(null)) } ``` ## 接口说明 1. 匹配所有`AllOf.allOf()` 2. 匹配某一个`AnyOf.AnyOf()` 3. 匹配每一个`Every.everyItem()` 4. 匹配任意`IsAnything.anything()` 5. 匹配不是`IsNot.not()` 6. 匹配null`IsNull.nullValue()` 7. 匹配包含字符串`StringContains.containsString()` 8. 匹配以字符串结尾`StringEndsWith.endsWith()` 9. 匹配以字符串开头`StringStartsWith.startsWith()` 10. 匹配空字符串`IsEmptyString.emptyString()` ## 约束与限制 在下述版本验证通过: - DevEco Studio 版本:4.1 Canary(4.1.3.317),OpenHarmony SDK:API11 (4.1.0.36) ## 目录结构 ```` |---- hamcrest | |---- entry # 示例代码文件夹 | |---- hamcrest # hamcrest库文件夹 | |---- index.ets # 对外接口 | |---- src | |---- main | |---- components | |---- beans # 核心文件夹 | |---- HasProperty.ts # 匹配属性 | |---- collection # 集合核心文件夹 | |---- ArrayMatching.ts # 数组匹配器 | |---- HasItemInArray.ts # 匹配数组是否包含某一个item | |---- IsArray.ts # 匹配是否是一个数组 | |---- IsArrayWithSize.ts # 匹配是某一个长度的数组 | |---- IsIn.ts # 某一个元素是否包含在某一个数组 | |---- IsMapContaining.ts # Map匹配器 | |---- IsMapWithSize.ts # 判断是某一个长度的Map | |---- core # 核心文件夹 | |---- Every.ts # 匹配每一个 | |---- AllOf.ts # 匹配所有 | |---- AnyOf.ts # 匹配任意 | |---- CombinableMatcher.ts # 组合匹配器 | |---- Is.ts # 是否是 | |---- IsEqual.ts # 是否相等 | |---- IsAnything.ts # 匹配任意 | |---- IsInstanceOf.ts # 类型匹配 | |---- IsIterableContaining.ts # 可迭代包含 | |---- IsNot.ts # 计算匹配器的逻辑否定 | |---- IsNull.ts # 是否是NUll | |---- IsSame.ts # 是不是一样 | |---- ShortcutCombination.ts # 快捷组合方式 | |---- StringContains.ts # 匹配包含字符串 | |---- StringEndsWith.ts # 是否是以特定子字符串结尾。 | |---- StringRegularExpression.ts # 字符串规则表达 | |---- StringStartsWith.ts # 是否是以特定子字符串开始。 | |---- SubstringMatcher.ts # 字符串截取匹配器 | |---- internal # 核心文件夹 | |---- ArrayIterator.ts # 数组迭代器 | |---- NullSafety.ts # null是否安全 | |---- SelfDescribingValue.ts # 自描述值 | |---- SelfDescribingValueIterator.ts # 自描述值迭代器 | |---- number # 核心文件夹 | |---- IsCloseTo.ts # 是否在一个区间 | |---- IsNaN.ts # 是否是NaN | |---- OrderingComparison.ts # 排序比较 | |---- text # 核心文件夹 | |---- CharSequenceLength.ts # 字符序列长度 | |---- IsBlankString.ts # 是否是空字符串 | |---- IsEmptyString.ts # 是否是空字符串 | |---- IsEqualCompressingWhiteSpace.ts # 是否等于压缩空格 | |---- IsEqualIgnoringCase.ts # 是否相等忽略大小写 | |---- MatchesPattern.ts # 匹配器模型 | |---- BaseDescription.ts # 描述基类 | |---- BaseMatcher.ts # 匹配器基类 | |---- CustomMatcher.ts # 自定义匹配器 | |---- Description.ts # 描述 | |---- DiagnosingMatcher.ts # 诊断匹配器 | |---- FeatureMatcher.ts # 特性匹配 | |---- Matcher.ts # 匹配器 | |---- MatcherAssert.ts #匹配器断言 | |---- NullDescription.ts # Null描述 | |---- SelfDescribing.ts # 描述自身的能力 | |---- StringDescription.ts # 字符串描述 | |---- README.md # 安装使用方法 | |---- README_zh.md # 安装使用方法 ```` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/Hamcrest/issues) 给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-sig/Hamcrest/pulls) 。 ## 开源协议 本项目基于 [BSD License](https://gitee.com/openharmony-sig/Hamcrest/blob/master/LICENSE) ,请自由地享受和参与开源。