Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit e02ad70

Browse files
authored
add TypeSpec::toString() and a bunch of unit tests to go along with it (#34)
* add TypeSpec::toString() Tests still in progress; many specs don't have tests yet. * Update FIXME codes * Make coerce(coerce(x)) a no-op for Maps * Add Vector test, based on Map test; fix bugs * SetSpec::toString() and don't change in coerceType if unneeded * Require 4.30, test on 4.30 and latest LTS * add tests for dict, keyset, and vec * Use fully-qualified name of dict/keyset/vec in toString() Otherwise they need to special-cased in any codegen in a namespace; using the fully-qualified name allows simply prefixing them with a `\` - however codegen can choose to special-case them for readability rather than being required to special-case for correctness. * use Traversable::class * Fix and test ShapeSpec also fix up KeyedTraversableSpec while I'm here
1 parent 5e61e30 commit e02ad70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+888
-124
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ sudo: required
22
language: generic
33
services: docker
44
env:
5-
- HHVM_VERSION=4.20-latest
5+
- HHVM_VERSION=4.30-latest
6+
- HHVM_VERSION=4.32-latest
67
- HHVM_VERSION=latest
78
- HHVM_VERSION=nightly
89
install:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"keywords": ["hack", "TypeAssert" ],
66
"require": {
7-
"hhvm": "^4.20",
7+
"hhvm": "^4.30",
88
"hhvm/hsl": "^4.0"
99
},
1010
"extra": {

src/TypeSpec.hack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ abstract class TypeSpec<+T> {
1515

1616
abstract public function coerceType(mixed $value): T;
1717
abstract public function assertType(mixed $value): T;
18+
abstract public function toString(): string;
1819

1920
public function isOptional(): bool {
2021
return false;

src/TypeSpec/__Private/ArrayKeySpec.hack

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ final class ArrayKeySpec extends UnionSpec<arraykey> {
1515
public function __construct() {
1616
parent::__construct('arraykey', new StringSpec(), new IntSpec());
1717
}
18+
19+
<<__Override>>
20+
public function toString(): string {
21+
return 'arraykey';
22+
}
1823
}

src/TypeSpec/__Private/BoolSpec.hack

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ final class BoolSpec extends TypeSpec<bool> {
3535
}
3636
throw IncorrectTypeException::withValue($this->getTrace(), 'bool', $value);
3737
}
38+
39+
<<__Override>>
40+
public function toString(): string {
41+
return 'bool';
42+
}
3843
}

src/TypeSpec/__Private/ClassnameSpec.hack

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ final class ClassnameSpec<Tinner, T as classname<Tinner>> extends TypeSpec<T> {
2828
throw
2929
IncorrectTypeException::withValue($this->getTrace(), $this->what, $value);
3030
}
31+
32+
<<__Override>>
33+
public function toString(): string {
34+
return 'classname<\\'.$this->what.'>';
35+
}
3136
}

src/TypeSpec/__Private/DictLikeArraySpec.hack

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Facebook\TypeSpec\__Private;
1313
use type Facebook\TypeAssert\{IncorrectTypeException, TypeCoercionException};
1414
use type Facebook\TypeSpec\TypeSpec;
1515

16-
use namespace HH\Lib\Dict;
16+
use namespace HH\Lib\{Dict, Str};
1717

1818
final class DictLikeArraySpec<Tk as arraykey, Tv>
1919
extends TypeSpec<array<Tk, Tv>> {
@@ -65,4 +65,13 @@ final class DictLikeArraySpec<Tk as arraykey, Tv>
6565
)
6666
|> /* HH_IGNORE_ERROR[4007] PHP array cast */ (array)$$;
6767
}
68+
69+
<<__Override>>
70+
public function toString(): string {
71+
return Str\format(
72+
'array<%s, %s>',
73+
$this->tsk->toString(),
74+
$this->tsv->toString(),
75+
);
76+
}
6877
}

src/TypeSpec/__Private/DictSpec.hack

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Facebook\TypeSpec\__Private;
1212

1313
use type Facebook\TypeAssert\{IncorrectTypeException, TypeCoercionException};
1414
use type Facebook\TypeSpec\TypeSpec;
15-
use namespace HH\Lib\Dict;
15+
use namespace HH\Lib\{Dict, Str};
1616

1717
final class DictSpec<Tk as arraykey, Tv> extends TypeSpec<dict<Tk, Tv>> {
1818

@@ -61,4 +61,14 @@ final class DictSpec<Tk as arraykey, Tv> extends TypeSpec<dict<Tk, Tv>> {
6161
($k, $_v) ==> $this->tsk->withTrace($kt)->assertType($k),
6262
);
6363
}
64+
65+
<<__Override>>
66+
public function toString(): string {
67+
return Str\format(
68+
'%s<%s, %s>',
69+
dict::class,
70+
$this->tsk->toString(),
71+
$this->tsv->toString(),
72+
);
73+
}
6474
}

src/TypeSpec/__Private/EnumSpec.hack

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ final class
4242
throw IncorrectTypeException::withValue($this->getTrace(), $what, $value);
4343
}
4444
}
45+
46+
<<__Override>>
47+
public function toString(): string {
48+
return $this->what;
49+
}
4550
}

src/TypeSpec/__Private/FloatSpec.hack

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ final class FloatSpec extends TypeSpec<float> {
6262
}
6363
throw IncorrectTypeException::withValue($this->getTrace(), 'float', $value);
6464
}
65+
66+
<<__Override>>
67+
public function toString(): string {
68+
return 'float';
69+
}
6570
}

0 commit comments

Comments
 (0)