tests: rewrite test with complex types

This commit is contained in:
2026-07-08 15:35:13 +02:00
parent 725e030374
commit ef9dd95844
2 changed files with 1575 additions and 1487 deletions

View File

@@ -9,26 +9,22 @@ type Longitude = float where (-180 <= _ <= 180)
type Difference[T] = T type Difference[T] = T
// Complex custom type, containing two values accessible through properties // Complex custom type, containing two values accessible through properties
type GeoLocation = { type GeoLocation = object
extend GeoLocation {
prop lat: Latitude prop lat: Latitude
prop lon: Longitude prop lon: Longitude
} }
// Define operations on our custom type type GeoLocationDifference = object
extend GeoLocation {
// This type is compatible with the `-` operation with another GeoLocation
// i.e. you can subtract a GeoLocation from another GeoLocation, resulting
// in a Difference of GeoLocations
def __sub__: fn(GeoLocation, /) -> Difference[GeoLocation]
}
// For complex generics, you need to specify how the genericity the properties extend GeoLocationDifference {
// are handled
type Difference[GeoLocation] = {
prop lat: Difference[Latitude] prop lat: Difference[Latitude]
prop lon: Difference[Longitude] prop lon: Difference[Longitude]
} }
// Define operations on our custom type
// Simple operation defined on our custom types // Simple operation defined on our custom types
extend Latitude { extend Latitude {
def __sub__: fn(Latitude, /) -> Difference[Latitude] def __sub__: fn(Latitude, /) -> Difference[Latitude]
@@ -38,13 +34,23 @@ extend Longitude {
def __sub__: fn(Longitude, /) -> Difference[Longitude] def __sub__: fn(Longitude, /) -> Difference[Longitude]
} }
extend GeoLocation {
// This type is compatible with the `-` operation with another GeoLocation
// i.e. you can subtract a GeoLocation from another GeoLocation, resulting
// in a GeoLocationDifference
def __sub__: fn(GeoLocation, /) -> GeoLocationDifference
}
// Predefined custom predicates that can be referenced in other definitions // Predefined custom predicates that can be referenced in other definitions
predicate Positive(v: float) = v >= 0 predicate Positive(v: float) = v >= 0
predicate StrictlyPositive(v: float) = v > 0 predicate StrictlyPositive(v: float) = v > 0
predicate Equatorial(loc: GeoLocation) = (-10 <= loc.lat <= 10) predicate Equatorial(loc: GeoLocation) = (-10 <= loc.lat <= 10)
predicate Arctic(loc: GeoLocation) = (loc.lat >= 66) predicate Arctic(loc: GeoLocation) = (loc.lat >= 66)
type Person = { type Person = object
extend Person {
prop name: str prop name: str
// Property with an inline constraint // Property with an inline constraint

File diff suppressed because it is too large Load Diff