Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from definiti/60-implicit-type
60 implicit type
- Loading branch information
Showing
14 changed files
with
354 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/resources/samples/definedType/implicit-attribute-type.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
type Person { | ||
email verifying IsNonEmptyEmail | ||
address | ||
} | ||
|
||
type Email = String | ||
|
||
type Address { | ||
city: String | ||
country: String | ||
} | ||
|
||
verification IsNonEmptyEmail { | ||
"Please give a non empty email" | ||
(email: Email) => { | ||
email.nonEmpty() | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/test/resources/samples/definedType/invalid-implicit-attribute-type.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type Person { | ||
address | ||
} |
3 changes: 3 additions & 0 deletions
3
src/test/resources/samples/namedFunction/implicit-lambda-parameter-type.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def contains[A](list: List[A], element: A): Boolean => { | ||
list.exists((a) => {a == element}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def nonEmpty(string): Boolean => { | ||
string.nonEmpty() | ||
} |
3 changes: 3 additions & 0 deletions
3
src/test/resources/samples/namedFunction/invalid-implicit-lambda-parameter-type.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def contains[A](list: List[A], element: A): Boolean => { | ||
list.exists((unknown) => {unknown == element}) | ||
} |
3 changes: 3 additions & 0 deletions
3
src/test/resources/samples/namedFunction/invalid-implicit-type.def
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def nonEmpty(unknown): Boolean => { | ||
unknown.nonEmpty() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
verification NonEmptyString { | ||
"The string should not be empty" | ||
(string) => { | ||
string.nonEmpty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
verification NonEmptyUnknown { | ||
"The unknown should not be empty" | ||
(unknown) => { | ||
unknown.nonEmpty() | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/test/scala/definiti/core/end2end/DefinedTypeSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package definiti.core.end2end | ||
|
||
import definiti.common.ast._ | ||
import definiti.common.program.{Ko, Ok} | ||
import definiti.common.tests.LocationPath | ||
import definiti.common.utils.ASTUtils._ | ||
import definiti.core.ProgramResultMatchers._ | ||
import definiti.core.validation.controls.{AttributeTypeControl, TypeDeclarationParametersControl} | ||
|
||
class DefinedTypeSpec extends EndToEndSpec { | ||
import DefinedTypeSpec._ | ||
|
||
"Project.generatePublicAST" should "generate the AST with a defined type with implicit attribute types" in { | ||
val expected = Ok[Root](implicitAttributeType) | ||
val output = processFile("definedType.implicit-attribute-type") | ||
output should beResult(expected) | ||
} | ||
|
||
it should "invalid the AST when an implicit type of an attribute does not exist" in { | ||
val expected = Ko[Root](invalidImplicitAttributeTypeErrors) | ||
val output = processFile("definedType.invalid-implicit-attribute-type") | ||
output should beResult(expected) | ||
} | ||
} | ||
|
||
object DefinedTypeSpec { | ||
val implicitAttributeTypeLocation = LocationPath("src/test/resources/samples/definedType/implicit-attribute-type.def") | ||
val implicitAttributeType = root( | ||
DefinedType( | ||
name = "Person", | ||
fullName = "Person", | ||
genericTypes = Seq.empty, | ||
parameters = Seq.empty, | ||
attributes = Seq( | ||
AttributeDefinition( | ||
name = "email", | ||
typeDeclaration = TypeDeclaration("Email", Seq.empty, Seq.empty, implicitAttributeTypeLocation(2, 3, 8)), | ||
comment = None, | ||
verifications = Seq( | ||
VerificationReference( | ||
verificationName = "IsNonEmptyEmail", | ||
parameters = Seq.empty, | ||
location = implicitAttributeTypeLocation(2, 9, 34) | ||
) | ||
), | ||
location = implicitAttributeTypeLocation(2, 3, 34) | ||
), | ||
AttributeDefinition( | ||
name = "address", | ||
typeDeclaration = TypeDeclaration("Address", Seq.empty, Seq.empty, implicitAttributeTypeLocation(3, 3, 10)), | ||
comment = None, | ||
verifications = Seq.empty, | ||
location = implicitAttributeTypeLocation(3, 3, 10) | ||
) | ||
), | ||
verifications = Seq.empty, | ||
inherited = Seq.empty, | ||
comment = None, | ||
location = implicitAttributeTypeLocation(1, 1, 4, 2) | ||
), | ||
AliasType( | ||
name = "Email", | ||
fullName = "Email", | ||
genericTypes = Seq.empty, | ||
parameters = Seq.empty, | ||
alias = TypeDeclaration("String", Seq.empty, Seq.empty, implicitAttributeTypeLocation(6, 14, 20)), | ||
inherited = Seq.empty, | ||
verifications = Seq.empty, | ||
comment = None, | ||
location = implicitAttributeTypeLocation(6, 1, 20) | ||
), | ||
DefinedType( | ||
name = "Address", | ||
fullName = "Address", | ||
genericTypes = Seq.empty, | ||
parameters = Seq.empty, | ||
attributes = Seq( | ||
AttributeDefinition( | ||
name = "city", | ||
typeDeclaration = TypeDeclaration("String", Seq.empty, Seq.empty, implicitAttributeTypeLocation(9, 9, 15)), | ||
comment = None, | ||
verifications = Seq.empty, | ||
location = implicitAttributeTypeLocation(9, 3, 15) | ||
), | ||
AttributeDefinition( | ||
name = "country", | ||
typeDeclaration = TypeDeclaration("String", Seq.empty, Seq.empty, implicitAttributeTypeLocation(10, 12, 18)), | ||
comment = None, | ||
verifications = Seq.empty, | ||
location = implicitAttributeTypeLocation(10, 3, 18) | ||
) | ||
), | ||
verifications = Seq.empty, | ||
inherited = Seq.empty, | ||
comment = None, | ||
location = implicitAttributeTypeLocation(8, 1, 11, 2) | ||
), | ||
Verification( | ||
name = "IsNonEmptyEmail", | ||
fullName = "IsNonEmptyEmail", | ||
parameters = Seq.empty, | ||
message = LiteralMessage("Please give a non empty email", implicitAttributeTypeLocation(14, 3, 34)), | ||
function = DefinedFunction( | ||
parameters = Seq(ParameterDefinition("email", TypeReference("Email"), implicitAttributeTypeLocation(15, 4, 16))), | ||
body = MethodCall( | ||
expression = Reference("email", TypeReference("Email"), implicitAttributeTypeLocation(16, 5, 10)), | ||
method = "nonEmpty", | ||
parameters = Seq.empty, | ||
generics = Seq.empty, | ||
returnType = TypeReference("Boolean"), | ||
location = implicitAttributeTypeLocation(16, 5, 21) | ||
), | ||
genericTypes = Seq.empty, | ||
location = implicitAttributeTypeLocation(15, 3, 17, 4) | ||
), | ||
comment = None, | ||
location = implicitAttributeTypeLocation(13, 1, 18, 2) | ||
) | ||
) | ||
|
||
val invalidImplicitAttributeTypeErrorsLocation = LocationPath("src/test/resources/samples/definedType/invalid-implicit-attribute-type.def") | ||
val invalidImplicitAttributeTypeErrors = Seq( | ||
AttributeTypeControl.errorUnknownType("Email", invalidImplicitAttributeTypeErrorsLocation(2, 3, 8)), | ||
AttributeTypeControl.errorUnknownType("Address", invalidImplicitAttributeTypeErrorsLocation(3, 3, 10)), | ||
TypeDeclarationParametersControl.errorUnknownType("Email", invalidImplicitAttributeTypeErrorsLocation(2, 3, 8)), | ||
TypeDeclarationParametersControl.errorUnknownType("Address", invalidImplicitAttributeTypeErrorsLocation(3, 3, 10)) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.