# 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 ## Introduction > Hamcrest is a library of matchers that can be combined for objects, strings, or numbers. ![operation.png](screenshots/operation.png) ## How to Install ```shell ohpm install @ohos/hamcrest ``` For details about the OpenHarmony ohpm environment configuration, see [OpenHarmony HAR](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.en.md). ## How to Use 1. Import files and code dependencies. ``` import {AllOf} from '@ohos/hamcrest' ``` 2. Build a matcher. ``` let matcher = AllOf.allOfMatches(StringContains.containsString('expected'), StringContains.containsString('value')) ``` 3. Call the matcher for matching. ``` 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", "Zhang San") let matcher = IsIterableContaining.hasItem(StringContains.containsString('Zhang San')) console.info(TAG + "isIterableContaining hasItem : " + matcher.matches(map)) let matcher1 = IsIterableContaining.hasItem(StringContains.containsString('Wang')) 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)) } ``` ## Available APIs 1. `AllOf.allOf()`: matches all conditions. 2. `AnyOf.AnyOf()`: matches one of the conditions. 3. `Every.everyItem()`: matches every condition. 4. `IsAnything.anything()`: matches any condition. 5. `IsNot.not()`: matches the false condition. 6. `IsNull.nullValue()`: matches a value that is null. 7. `StringContains.containsString()`: matches a text that contains a specific string. 8. `StringEndsWith.endsWith()`: matches a text that ends with a specific string. 9. `StringStartsWith.startsWith()`: matches a text that starts with a specific string. 10. `IsEmptyString.emptyString()`: matches a string that is empty. ## Constraints This project has been verified in the following version: - DevEco Studio: 4.1 Canary (4.1.3.317), OpenHarmony SDK: API 11 (4.1.0.36) ## Directory Structure ```` |---- hamcrest | |---- entry # Sample code | |---- hamcrest # Hamcrest library | |---- index.ets # External APIs | |---- src | |---- main | |---- components | |---- beans # Core directory | |---- HasProperty.ts # Property matcher | |---- collection # Core directory | |---- ArrayMatching.ts # Array matcher | |---- HasItemInArray.ts # Checks whether an array contains a specific item | |---- IsArray.ts # Checks whether a target is an array | |---- IsArrayWithSize.ts # Matches an array of a specific length | |---- IsIn.ts # Checks whether an element is contained in an array | |---- IsMapContaining.ts # Map matcher | |---- IsMapWithSize.ts # Checks a Map of a specific length | |---- core # Core directory | |---- Every.ts # Matches every condition | |---- AllOf.ts # Matches all conditions | |---- AnyOf.ts # Matches one of the conditions | |---- CombinableMatcher.ts # Combinable matcher | |---- Is.ts # Checks whether the condition is true | |---- IsEqual.ts # Checks whether the values are equal | |---- IsAnything.ts # Matches any condition | |---- IsInstanceOf.ts # Checks whether an object is an instance of a particular class | |---- IsIterableContaining.ts # Checks whether an object is iterable and contained | |---- IsNot.ts # Checks whether the condition is false | |---- IsNull.ts # Checks whether an instance is null | |---- IsSame.ts # Checks whether the values are the same | |---- ShortcutCombination.ts # Shortcut combination | |---- StringContains.ts # Matches a text that contains a specific string | |---- StringEndsWith.ts # Matches a text that ends with a specific string | |---- StringRegularExpression.ts # Regular expressions of the string | |---- StringStartsWith.ts # Matches a text that starts with a specific string | |---- SubstringMatcher.ts # Truncated string matcher | |---- internal # Core directory | |---- ArrayIterator.ts # Array iterator | |---- NullSafety.ts # Checks whether a null is safe | |---- SelfDescribingValue.ts # Self-describing value | |---- SelfDescribingValueIterator.ts # Self-describing value iterator | |---- number # Core directory | |---- IsCloseTo.ts # Checks whether the numbers are in the same range of acceptable error | |---- IsNaN.ts # Checks whether a number is NaN | |---- OrderingComparison.ts # Sorts or compares numbers | |---- text # Core directory | |---- CharSequenceLength.ts # Checks the length of a character sequence | |---- IsBlankString.ts # Checks whether a string is blank | |---- IsEmptyString.ts # Checks whether a string is empty | |---- IsEqualCompressingWhiteSpace.ts # Checks whether a string is equal to another string, compressing any changes in whitespace | |---- IsEqualIgnoringCase.ts # Checks whether a text string is equal to another string, regardless of the case | |---- MatchesPattern.ts # Matcher model | |---- BaseDescription.ts # Base description | |---- BaseMatcher.ts # Base matcher | |---- CustomMatcher.ts # Custom matcher | |---- Description.ts # Description | |---- DiagnosingMatcher.ts # Diagnosis matcher | |---- FeatureMatcher.ts # Feature matcher | |---- Matcher.ts # Matcher | |---- MatcherAssert.ts # Matcher assert | |---- NullDescription.ts # Null description | |---- SelfDescribing.ts # Capability of self-describing | |---- StringDescription.ts # String description | |---- README.md # Readme | |---- README_zh.md # Readme ```` ## How to Contribute If you find any problem when using the project, submit an [issue](https://gitee.com/openharmony-sig/Hamcrest/issues) or a [PR](https://gitee.com/openharmony-sig/Hamcrest/pulls). ## License This project is licensed under [BSD License](https://gitee.com/openharmony-sig/Hamcrest/blob/master/LICENSE).