用户可使用该项目进行数据匹配与验证,它提供多种匹配器,支持字符串、数值、数组等数据类型的匹配,还能组合匹配条件,满足复杂验证需求。【此简介由AI生成】
| Files | Last commit | Last update |
|---|---|---|
| 3 years ago | ||
| 1 year ago | ||
| 2 years ago | ||
| 5 months ago | ||
| 4 years ago | ||
| 2 years ago | ||
| 1 year ago | ||
| 4 years ago | ||
| 3 years ago | ||
| 2 years ago | ||
| 2 years ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 2 years ago | ||
| 1 year ago | ||
| 3 years ago | ||
| 2 years ago | ||
| 2 years ago | ||
| 1 year ago |
hamcrest
Introduction
Hamcrest is a library of matchers that can be combined for objects, strings, or numbers.

How to Install
ohpm install @ohos/hamcrest
For details about the OpenHarmony ohpm environment configuration, see OpenHarmony HAR.
How to Use
-
Import files and code dependencies.
import {AllOf} from '@ohos/hamcrest' -
Build a matcher.
let matcher = AllOf.allOfMatches(StringContains.containsString('expected'), StringContains.containsString('value')) -
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<ESObject> = 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<ESObject> = IsIn.isIn(["bar", "foo"]) console.info(TAG + "isIn: " + matcher.matches("bar")) console.info(TAG + "isIn: " + matcher.matches(["bar", "foo"])) } every() { let matcher: Matcher<Array<ESObject>> = 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<String> = new Array(1) arr[0] = "hello" let arr1: Array<String> = 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<String, String> = 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<String>= 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
AllOf.allOf(): matches all conditions.AnyOf.AnyOf(): matches one of the conditions.Every.everyItem(): matches every condition.IsAnything.anything(): matches any condition.IsNot.not(): matches the false condition.IsNull.nullValue(): matches a value that is null.StringContains.containsString(): matches a text that contains a specific string.StringEndsWith.endsWith(): matches a text that ends with a specific string.StringStartsWith.startsWith(): matches a text that starts with a specific string.IsEmptyString.emptyString(): matches a string that is empty.
About obfuscation
- Code obfuscation, please seeCode Obfuscation
- If you want the hamcrest library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/hamcrest
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 or a PR.
License
This project is licensed under BSD License.
Introduction
用户可使用该项目进行数据匹配与验证,它提供多种匹配器,支持字符串、数值、数组等数据类型的匹配,还能组合匹配条件,满足复杂验证需求。【此简介由AI生成】
Customize your domain