Skip to content

Commit

Permalink
Draft: Fix tests (#23)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced flexibility in the `Overwrite` function by allowing implicit
type conversion for the `value` argument.
  
- **Bug Fixes**
- Updated type constraints in `Overwrite` constructor to ensure better
compatibility and error handling.

- **Tests**
- Adjusted test cases to align with updated type constraints, improving
overall test accuracy.
  
- **Refactor**
- Refined type restrictions in various function calls and declarations
for improved code clarity and performance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
miRoox authored Jun 18, 2024
1 parent 5ebd2ea commit ac905be
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Overwrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ julia> deserialize(Overwrite(UInt8, 0x01), b"\\x05")
```
"""
Overwrite(subcon::Construct{T}, value::T) where {T} = Overwrite(subcon, ((obj; contextkw...) -> value))
Overwrite(subcon::Construct{T}, value) where {T} = Overwrite(subcon, ((obj; contextkw...) -> convert(T, value)))
Overwrite(::Type{T}, getter) where {T} = Overwrite(Construct(T), getter)

encode(cons::Overwrite{T, TSubCon, GT}, obj::T; contextkw...) where {T, TSubCon, GT<:Function} = convert(T, apply_optional_contextkw(cons.getter, obj, contextkw))
Expand Down
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ end
@testset "Overwrite" begin
@testset "deduce type" begin
@test Constructs.deducetype((v) -> Overwrite(UInt8, v), UInt8) <: Construct{UInt8}
@test Constructs.deducetype((v) -> Overwrite(UInt8, v), Function) <: Construct{UInt8}
@test Constructs.deducetype((v) -> Overwrite(UInt8, v), UndefProperty) <: Construct{UInt8}
@test Constructs.deducetype((v) -> Overwrite(Int8, v), typeof(abs)) <: Construct{Int8}
@test Constructs.deducetype((v) -> Overwrite(UInt8, v), UndefProperty{UInt8}) <: Construct{UInt8}
end
@test_throws ArgumentError Overwrite(UInt8, () -> 0x01)
@test serialize(Overwrite(UInt8, 0x01), 2) == b"\x01"
Expand Down Expand Up @@ -643,17 +643,17 @@ end
withoverwrite = quote
@construct struct Bitmap <: AbstractImage
signature::Const(b"BMP")
width::Overwrite(UInt32, convert(UInt32, size(this.pixel, 2)))
height::Overwrite(UInt32, convert(UInt32, size(this.pixel, 1)))
width::Overwrite(UInt32, _ -> convert(UInt32, size(this.pixel, 2)))
height::Overwrite(UInt32, _ -> convert(UInt32, size(this.pixel, 1)))
::Padded(8)
pixel::SizedArray(UInt8, this.height, this.width)
end
end
withhidden = quote
@construct struct Bitmap <: AbstractImage
[signature]::Const(b"BMP")
[width]::Overwrite(UInt32, convert(UInt32, size(this.pixel, 2)))
[height]::Overwrite(UInt32, convert(UInt32, size(this.pixel, 1)))
[width]::Overwrite(UInt32, _ -> convert(UInt32, size(this.pixel, 2)))
[height]::Overwrite(UInt32, _ -> convert(UInt32, size(this.pixel, 1)))
::Padded(8)
pixel::SizedArray(UInt8, this.height, this.width)
end
Expand Down

0 comments on commit ac905be

Please sign in to comment.