Skip to content

Commit 85b591a

Browse files
committed
Testing by reproducing HSA patterns
1 parent 48394ba commit 85b591a

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Examples: `->dot("zeroOrMore")` `->exact("hello worls", false, "1+")`
5757
- negativeLookAhead(callable $callback): Negative lookahead assertion.✔️
5858
- negativeLookBehind(callable $callback): Negative lookbehind assertion.✔️
5959
- Raw regex methods for advanced users.✔️
60-
- BuilderPattern should be able to reproduce patterns used in HSA
60+
- BuilderPattern should be able to reproduce patterns used in HSA✔️
6161

6262
- Add benchmarks and tests for search against large data ✔️
6363
- Add Feature Tests for BuilderPattern

src/Patterns/BuilderPattern.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ private function getLengthOption($length = null, $minLength = 0, $maxLength = 0)
6464
return "+"; // Default case, one or more times
6565
}
6666

67+
public function set(callable $callback): self {
68+
$subPattern = new self();
69+
$callback($subPattern);
70+
$this->pattern .= '[' . $subPattern->getPattern() . ']';
71+
return $this;
72+
}
73+
6774
public function group(callable $callback): self {
6875
$subPattern = new self();
6976
$callback($subPattern);

src/Traits/BuilderPatternTraits/SpecificCharsTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public function forwardSlash(): self {
7171
return $this->escapeAndAdd("/");
7272
}
7373

74+
public function slash(): self {
75+
return $this->escapeAndAdd("/");
76+
}
77+
7478
public function underscore(): self {
7579
return $this->escapeAndAdd("_");
7680
}
@@ -205,6 +209,10 @@ public function hash(): self {
205209
return $this->escapeAndAdd("#");
206210
}
207211

212+
public function hashtag(): self {
213+
return $this->escapeAndAdd("#");
214+
}
215+
208216
public function backtick(): self {
209217
return $this->escapeAndAdd("`");
210218
}

tests/Feature/BuilderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,37 @@
22

33
use Maestroerror\EloquentRegex\Builder;
44

5+
it('reproduces alt prefix pattern from HSA', function () {
6+
$builder = new Builder("");
7+
8+
$regex = $builder->start()->exact("alt=")->group(function ($pattern) {
9+
$pattern->doubleQuote()->orPattern(function ($pattern) {
10+
$pattern->singleQuote();
11+
});
12+
})->end()->toRegex();
13+
14+
15+
expect($regex)->toBe("alt\=(\"|')");
16+
});
17+
18+
it('reproduces hashtag prefix pattern from HSA', function () {
19+
$builder = new Builder("");
20+
21+
$regex = $builder->start()->lookBehind(function ($pattern) {
22+
$pattern->set(function ($pattern) {
23+
$pattern->doubleQuote()->closeAngleBracket()->addRawRegex("\\s");
24+
});
25+
})->hash()->end()->toRegex();
26+
27+
expect($regex)->toBe('(?<=["\>\s])\#');
28+
});
29+
30+
it('reproduces Text suffix pattern from HSA', function () {
31+
$builder = new Builder("");
32+
33+
$regex = $builder->start()
34+
->openAngleBracket()->slash()->alphanumericRange(0, 10)->closeAngleBracket()
35+
->end()->toRegex();
36+
37+
expect($regex)->toBe('\<\/[a-zA-Z0-9]{0,10}\>');
38+
});

0 commit comments

Comments
 (0)