Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#67] Restrict the type equality on alias types
Currently, when we use an alias type, we check that the final alias types are the same. However, types should ensure we compare two same things. So we restrict the control over alias type to the alias itself. This commit does the following: * Type equality does not try to check the final type but use the direct type itself * Change `EqualityOnSameTypeControl` into `ComparisonOnSameTypeControl` because the control should be on inequalities too * Add a test for calculator operands and alias types * Move and add tests for comparison on same type with alias types too * Add control that function parameters uses the same alias type * Add control that method parameters uses the same alias type * Update test from `typeVerificationIsOkKo` to use the alias type instead of its alias
- Loading branch information
Gaëtan Rizio
committed
Aug 5, 2018
1 parent
ce64e25
commit cef53a4
Showing
17 changed files
with
138 additions
and
72 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
14 changes: 14 additions & 0 deletions
14
src/test/resources/samples/controls/calculatorOperandsAreNumber/validAliasType.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,14 @@ | ||
type Amount = Number | ||
type Rate = Number | ||
|
||
def plus(amount1: Amount, amount2: Amount): Number => { | ||
amount1 + amount2 | ||
} | ||
|
||
def minus(amount1: Amount, amount2: Number): Number => { | ||
amount1 + amount2 | ||
} | ||
|
||
def times(amount1: Amount, rate: Rate): Number => { | ||
amount1 * rate | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/resources/samples/controls/comparisonOnSameType/invalidAliasType.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,9 @@ | ||
type Amount = Number | ||
|
||
def isNotZero(value: Amount): Boolean => { | ||
value == 0 | ||
} | ||
|
||
def isPositive(value: Amount): Boolean => { | ||
value > 0 | ||
} |
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
src/test/resources/samples/controls/comparisonOnSameType/invalidDoubleAliasType.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,10 @@ | ||
type Amount = Number | ||
type Rate = Number | ||
|
||
def isEqual(value: Amount, rate: Rate): Boolean => { | ||
value != rate | ||
} | ||
|
||
def isLower(value: Amount, rate: Rate): Boolean => { | ||
value < rate | ||
} |
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
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
src/test/resources/samples/controls/functionParameters/invalidAliasTypeReference.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,9 @@ | ||
type Title = String | ||
|
||
def isEmptyTitle(title: Title): Boolean => { | ||
title.nonEmpty() | ||
} | ||
|
||
def isEmpty(string: String): Boolean => { | ||
isEmptyTitle(string) | ||
} |
5 changes: 5 additions & 0 deletions
5
src/test/resources/samples/controls/methodParameters/invalidAliasTypeReference.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,5 @@ | ||
type Substring = String | ||
|
||
def contains(substring: Substring): Boolean => { | ||
"Some text".contains(substring) | ||
} |
2 changes: 1 addition & 1 deletion
2
src/test/resources/samples/controls/typeVerificationIsOkKo/nominal.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
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
53 changes: 53 additions & 0 deletions
53
src/test/scala/definiti/core/end2end/controls/ComparisonOnSameTypeControlSpec.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,53 @@ | ||
package definiti.core.end2end.controls | ||
|
||
import definiti.common.ast.{Root, TypeReference} | ||
import definiti.common.tests.{ConfigurationMock, LocationPath} | ||
import definiti.core.Constants | ||
import definiti.core.ProgramResultMatchers._ | ||
import definiti.core.end2end.EndToEndSpec | ||
import definiti.core.validation.controls.ComparisonOnSameTypeControl | ||
|
||
class ComparisonOnSameTypeControlSpec extends EndToEndSpec { | ||
import ComparisonOnSameTypeControlSpec._ | ||
|
||
"Project.generatePublicAST" should "validate an expression when equality operands are both the same typoe" in { | ||
val output = processFile("controls.comparisonOnSameType.nominal", configuration) | ||
output shouldBe ok[Root] | ||
} | ||
|
||
it should "invalidate an expression when equality operands are not the same type" in { | ||
val output = processFile("controls.comparisonOnSameType.nominalInvalid", configuration) | ||
output shouldBe ko[Root] | ||
} | ||
|
||
it should "invalidate an invalid expression in a condition" in { | ||
val output = processFile("controls.comparisonOnSameType.invalidCondition", configuration) | ||
output should beKo( | ||
ComparisonOnSameTypeControl.errorDifferentTypes(Constants.number, Constants.string, invalidConditionLocation(2, 7, 37)) | ||
) | ||
} | ||
|
||
it should "invalidate a condition between an alias type and a type" in { | ||
val output = processFile("controls.comparisonOnSameType.invalidAliasType", configuration) | ||
output should beKo( | ||
ComparisonOnSameTypeControl.errorDifferentTypes(TypeReference("Amount"), Constants.number, invalidAliasTypeLocation(4, 3, 13)), | ||
ComparisonOnSameTypeControl.errorDifferentTypes(TypeReference("Amount"), Constants.number, invalidAliasTypeLocation(8, 3, 12)) | ||
) | ||
} | ||
|
||
it should "invalidate a condition between two alias types" in { | ||
val output = processFile("controls.comparisonOnSameType.invalidDoubleAliasType", configuration) | ||
output should beKo( | ||
ComparisonOnSameTypeControl.errorDifferentTypes(TypeReference("Amount"), TypeReference("Rate"), invalidDoubleAliasTypeLocation(5, 3, 16)), | ||
ComparisonOnSameTypeControl.errorDifferentTypes(TypeReference("Amount"), TypeReference("Rate"), invalidDoubleAliasTypeLocation(9, 3, 15)) | ||
) | ||
} | ||
} | ||
|
||
object ComparisonOnSameTypeControlSpec { | ||
val configuration = ConfigurationMock().withOnlyControls(ComparisonOnSameTypeControl) | ||
|
||
val invalidConditionLocation = LocationPath.control(ComparisonOnSameTypeControl, "invalidCondition") | ||
val invalidAliasTypeLocation = LocationPath.control(ComparisonOnSameTypeControl, "invalidAliasType") | ||
val invalidDoubleAliasTypeLocation = LocationPath.control(ComparisonOnSameTypeControl, "invalidDoubleAliasType") | ||
} |
35 changes: 0 additions & 35 deletions
35
src/test/scala/definiti/core/end2end/controls/EqualityOnSameTypeControlSpec.scala
This file was deleted.
Oops, something went wrong.
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