|
| 1 | +refdir = joinpath(refroot, "fileio") |
| 2 | + |
| 3 | +@testset "query" begin |
| 4 | + check_types = [ |
| 5 | + # text types |
| 6 | + ("textfile_with_no_extension", format"TXT"), |
| 7 | + ("textfile.txt", format"TXT"), |
| 8 | + ("textfile.unknown", format"TXT"), |
| 9 | + ("textfile.sha256", format"SHA256"), |
| 10 | + |
| 11 | + # image types |
| 12 | + ("imagefile.jpg", format"JPEG"), |
| 13 | + ("imagefile.jpeg", format"JPEG"), |
| 14 | + ("imagefile.png", format"PNG"), |
| 15 | + ("imagefile.tif", format"TIFF"), |
| 16 | + ("imagefile.tiff", format"TIFF"), |
| 17 | + |
| 18 | + # dataframe types |
| 19 | + ("dataframe_file.csv", format"CSV") |
| 20 | + ] |
| 21 | + for (file, fmt) in check_types |
| 22 | + @test ReferenceTests.query_extended(file) == File{fmt}(file) |
| 23 | + @test ReferenceTests.query_extended(abspath(file)) == File{fmt}(abspath(file)) |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +@testset "maybe_encode" begin |
| 28 | + @testset "string" begin |
| 29 | + str1 = "Hello world" |
| 30 | + str1_sha256 = "64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c" |
| 31 | + str2 = "Hello\n world" |
| 32 | + str2_sha256 = "60b65ab310480818c4289227f2ec68f1714743db8571b4cb190e100c0085be3d" # bytes2hex(SHA.sha256(str2)) |
| 33 | + str2_crlf = "Hello\r\n world" |
| 34 | + str3 = "Hello\nworld" |
| 35 | + str3_sha256 = "46e0ea795802f17d0b340983ca7d7068c94d7d9172ee4daea37a1ab1168649ec" # bytes2hex(SHA.sha256(str3)) |
| 36 | + str3_arr1 = ["Hello", "world"] |
| 37 | + str3_arr2 = ["Hello" "world"] |
| 38 | + str4 = "Hello\n world1\nHello\n world2" |
| 39 | + str4_sha256 = "c7dc8b82c3a6fed4afa0c8790a0586b73df0e4f35524efe6810e5d78b6b6a611" # bytes2hex(SHA.sha256(str4)) |
| 40 | + str4_arr = ["Hello\r\n world1", "Hello\n world2"] |
| 41 | + |
| 42 | + # string as plain text |
| 43 | + fmt = format"TXT" |
| 44 | + # convert should respect whitespaces |
| 45 | + @test str1 == ReferenceTests.maybe_encode(fmt, str1) |
| 46 | + @test str2 == ReferenceTests.maybe_encode(fmt, str2) |
| 47 | + # but ignore CRLF/LF differences |
| 48 | + @test str2 == ReferenceTests.maybe_encode(fmt, str2_crlf) |
| 49 | + # string arrays are treated as multi-line strings, even for UNKNOWN format |
| 50 | + @test str3 == ReferenceTests.maybe_encode(fmt, str3) |
| 51 | + @test str3 == ReferenceTests.maybe_encode(fmt, str3_arr1) |
| 52 | + @test str3 == ReferenceTests.maybe_encode(fmt, str3_arr2) |
| 53 | + # string arrays should ignore CRLF/LF differences, too |
| 54 | + @test str4 == ReferenceTests.maybe_encode(fmt, str4_arr) |
| 55 | + |
| 56 | + # string as SHA256 should also ignore CRLF/LF differences |
| 57 | + fmt = format"SHA256" |
| 58 | + @test str1_sha256 == ReferenceTests.maybe_encode(fmt, str1) |
| 59 | + @test str2_sha256 == ReferenceTests.maybe_encode(fmt, str2) |
| 60 | + # but ignore CRLF/LF differences |
| 61 | + @test str2_sha256 == ReferenceTests.maybe_encode(fmt, str2_crlf) |
| 62 | + # string arrays are treated as multi-line strings, even for UNKNOWN format |
| 63 | + @test str3_sha256 == ReferenceTests.maybe_encode(fmt, str3) |
| 64 | + @test str3_sha256 == ReferenceTests.maybe_encode(fmt, str3_arr1) |
| 65 | + @test str3_sha256 == ReferenceTests.maybe_encode(fmt, str3_arr2) |
| 66 | + # string arrays should ignore CRLF/LF differences, too |
| 67 | + @test str4_sha256 == ReferenceTests.maybe_encode(fmt, str4_arr) |
| 68 | + |
| 69 | + # unknown formats |
| 70 | + fmt = format"PNG" |
| 71 | + for str in (str1, str2, str2_crlf, str3, str3_arr1, str3_arr2) |
| 72 | + @test str === ReferenceTests.maybe_encode(fmt, str) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + @testset "numbers" begin |
| 77 | + for num in (0x01, 1, 1.0f0, 1.0) |
| 78 | + for fmt in (format"TXT", format"UNKNOWN") |
| 79 | + @test num === ReferenceTests.maybe_encode(fmt, num) |
| 80 | + end |
| 81 | + fmt = format"SHA256" |
| 82 | + @test ReferenceTests.maybe_encode(fmt, num) == ReferenceTests.maybe_encode(fmt, string(num)) |
| 83 | + end |
| 84 | + |
| 85 | + |
| 86 | + for (fmt, a, ref) in [ |
| 87 | + # if target is TXT, convert it to string |
| 88 | + (format"TXT", [1, 2], "[1, 2]"), |
| 89 | + (format"TXT", [1,2], "[1, 2]"), |
| 90 | + (format"TXT", [1;2], "[1, 2]"), |
| 91 | + (format"TXT", [1 2], "[1 2]"), |
| 92 | + (format"TXT", [1 2; 3 4], "[1 2; 3 4]"), |
| 93 | + # if target is Unknown, make no change |
| 94 | + (format"UNKNOWN", [1, 2], [1, 2]), |
| 95 | + (format"UNKNOWN", [1,2], [1, 2]), |
| 96 | + (format"UNKNOWN", [1;2], [1, 2]), |
| 97 | + (format"UNKNOWN", [1 2], [1 2]), |
| 98 | + (format"UNKNOWN", [1 2; 3 4], [1 2; 3 4]), |
| 99 | + ] |
| 100 | + @test ref == ReferenceTests.maybe_encode(fmt, a) |
| 101 | + end |
| 102 | + |
| 103 | + for a in [[1, 2], [1 2], [1 2; 3 4]] |
| 104 | + fmt = format"SHA256" |
| 105 | + @test ReferenceTests.maybe_encode(fmt, a) == ReferenceTests.maybe_encode(fmt, string(a)) |
| 106 | + end |
| 107 | + |
| 108 | + end |
| 109 | + |
| 110 | + @testset "image" begin |
| 111 | + gray_1d = Gray{N0f8}.(0.0:0.1:0.9) |
| 112 | + rgb_1d = RGB.(gray_1d) |
| 113 | + gray_2d = Gray{N0f8}.(reshape(0.0:0.1:0.9, 2, 5)) |
| 114 | + rgb_2d = RGB.(gray_2d) |
| 115 | + gray_3d = Gray{N0f8}.(reshape(0.0:0.02:0.95, 2, 4, 6)) |
| 116 | + rgb_3d = RGB.(gray_3d) |
| 117 | + |
| 118 | + # any common image types |
| 119 | + for img in (gray_1d, gray_2d, gray_3d, rgb_1d, rgb_2d, rgb_3d) |
| 120 | + for fmt in (format"JPEG", format"PNG", format"TIFF", format"UNKNOWN") |
| 121 | + @test img === ReferenceTests.maybe_encode(fmt, img) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + # image as text file |
| 126 | + fmt = format"TXT" |
| 127 | + # TODO: support n-D image encoding |
| 128 | + # @test_reference joinpath(refdir, "gray_1d_as_txt.txt") ReferenceTests.maybe_encode(fmt, gray_1d) |
| 129 | + # @test_reference joinpath(refdir, "rgb_1d_as_txt.txt") ReferenceTests.maybe_encode(fmt, rgb_1d) |
| 130 | + @test_reference joinpath(refdir, "gray_2d_as_txt.txt") ReferenceTests.maybe_encode(fmt, gray_2d) |
| 131 | + @test_reference joinpath(refdir, "rgb_2d_as_txt.txt") ReferenceTests.maybe_encode(fmt, rgb_2d) |
| 132 | + # @test_reference joinpath(refdir, "gray_3d_as_txt.txt") ReferenceTests.maybe_encode(fmt, gray_3d) |
| 133 | + # @test_reference joinpath(refdir, "rgb_3d_as_txt.txt") ReferenceTests.maybe_encode(fmt, rgb_3d) |
| 134 | + |
| 135 | + # image as SHA256 |
| 136 | + fmt = format"SHA256" |
| 137 | + for (file, img) in [ |
| 138 | + ("gray_1d", gray_1d), |
| 139 | + ("gray_2d", gray_2d), |
| 140 | + ("gray_3d", gray_3d), |
| 141 | + ("rgb_1d", rgb_1d), |
| 142 | + ("rgb_2d", rgb_2d), |
| 143 | + ("rgb_3d", rgb_3d) |
| 144 | + ] |
| 145 | + reffile = joinpath(refdir, "$(file)_as_sha256.txt") |
| 146 | + @test_reference reffile ReferenceTests.maybe_encode(fmt, img) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + # dataframe |
| 151 | + @testset "dataframe" begin |
| 152 | + df = DataFrame(v1=[1,2,3], v2=["a","b","c"]) |
| 153 | + |
| 154 | + @test string(df) == ReferenceTests.maybe_encode(format"TXT", df) |
| 155 | + for fmt in (format"CSV", format"UNKNOWN") |
| 156 | + @test df === ReferenceTests.maybe_encode(fmt, df) |
| 157 | + end |
| 158 | + |
| 159 | + fmt = format"SHA256" |
| 160 | + @test_reference joinpath(refdir, "dataframe_as_sha256.txt") ReferenceTests.maybe_encode(fmt, df) |
| 161 | + |
| 162 | + end |
| 163 | +end |
| 164 | + |
| 165 | +# TODO: savefile & loadfile |
0 commit comments