From b323e3febbacb29b269265aeaf1677d61685c4f2 Mon Sep 17 00:00:00 2001 From: Christopher Doris Date: Fri, 31 Jan 2025 17:12:15 +0000 Subject: [PATCH] Add README examples test suite for Chevrons --- test/integration.jl | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/integration.jl b/test/integration.jl index 5167352..6156915 100644 --- a/test/integration.jl +++ b/test/integration.jl @@ -45,3 +45,59 @@ end @map({number_of_children = __.children, __.age})() >> DataFrame() ) == Data.df1b end + +@testitem "README examples" begin + using DataFrames, TidierData, Test + + @testset "Basic DataFrame example" begin + df = DataFrame( + name = ["John", "Sally", "Roger"], + age = [54, 34, 79], + children = [0, 2, 4], + ) + result = @chevrons df >> @filter(age > 40) >> @select(num_children = children, age) + @test size(result) == (2, 2) + @test result.age == [54, 79] + @test result.num_children == [0, 4] + end + + @testset "Basic array manipulation" begin + result = @chevrons Int[] >> push!(5, 2, 4, 3, 1) >> sort!() + @test result == [1, 2, 3, 4, 5] + end + + @testset "Filter with isodd" begin + result = @chevrons [5, 2, 4, 3, 1] >> filter!(isodd, _) + @test result == [5, 3, 1] + end + + @testset "Filter with expression involving _" begin + result = @chevrons [5, 2, 4, 3, 1] >> filter!(isodd, _ .+ 10) + @test result == [15, 13, 11] + end + + @testset "Side effects with >>>" begin + x = 0 + result = @chevrons 10 >> (_ * 2) >>> (x = _) >> (x^2 - _) + @test result == 380 + @test x == 20 + end + + @testset "Array mutation with >>>" begin + arr = [5, 2, 4, 3, 1] + result = @chevrons arr >>> popat!(4) + @test result == arr # >>> returns the original array + @test arr == [5, 2, 4, 1] # verify mutation worked + end + + @testset "Backwards piping with <<" begin + mktempdir() do dir + path = joinpath(dir, "test.txt") + write(path, "ignore this line\nkeep this line!") + result = @chevrons ( + path >> open() << (io -> io >>> readline() >> read(String)) >> uppercase() + ) + @test result == "KEEP THIS LINE!" + end + end +end