aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2022-07-27 12:27:07 +0200
committerGitHub <noreply@github.com>2022-07-27 12:27:07 +0200
commit7c24e0f445234bd4b1b8552586999bafcad9f4f4 (patch)
treeda5a5b29a0a7787bb39e57e405cb78ebf58340f8
parentbb30f33d507f9aa81a725d70b202821d634cd044 (diff)
parent60d9106f5ed1834ebe62862ed9aba395e080c6bd (diff)
Merge pull request #123 from nico-bb/master
odinfmt: Refactor how the test outputs are compared with their respective snapshots
-rw-r--r--tools/odinfmt/snapshot/snapshot.odin34
1 files changed, 29 insertions, 5 deletions
diff --git a/tools/odinfmt/snapshot/snapshot.odin b/tools/odinfmt/snapshot/snapshot.odin
index 42f8034..ba5924f 100644
--- a/tools/odinfmt/snapshot/snapshot.odin
+++ b/tools/odinfmt/snapshot/snapshot.odin
@@ -4,6 +4,7 @@ import "core:testing"
import "core:os"
import "core:path/filepath"
import "core:strings"
+import "core:text/scanner"
import "core:fmt"
import "shared:odin/format"
@@ -59,11 +60,34 @@ snapshot_file :: proc(path: string) -> bool {
if os.exists(snapshot_path) {
if snapshot_data, ok := os.read_entire_file(snapshot_path, context.temp_allocator); ok {
- if cast(string)snapshot_data != formatted {
- fmt.eprintf("\nFormatted file was different from snapshot file: %v", snapshot_path)
- os.write_entire_file(fmt.tprintf("%v_failed", snapshot_path), transmute([]u8)formatted)
- return false
- }
+ snapshot_scanner := scanner.Scanner {}
+ scanner.init(&snapshot_scanner, string(snapshot_data))
+ formatted_scanner := scanner.Scanner {}
+ scanner.init(&formatted_scanner, string(formatted))
+ for {
+ s_ch := scanner.next(&snapshot_scanner)
+ f_ch := scanner.next(&formatted_scanner)
+ if s_ch == scanner.EOF || f_ch == scanner.EOF {
+ break
+ }
+
+ if s_ch == '\r' {
+ if scanner.peek(&snapshot_scanner) == '\n' {
+ s_ch = scanner.next(&snapshot_scanner)
+ }
+ }
+ if f_ch == '\r' {
+ if scanner.peek(&formatted_scanner) == '\n' {
+ f_ch = scanner.next(&formatted_scanner)
+ }
+ }
+
+ if s_ch != f_ch {
+ fmt.eprintf("\nFormatted file was different from snapshot file: %v", snapshot_path)
+ os.write_entire_file(fmt.tprintf("%v_failed", snapshot_path), transmute([]u8)formatted)
+ return false
+ }
+ }
os.remove(fmt.tprintf("%v_failed", snapshot_path))
} else {
fmt.eprintf("Failed to read snapshot file %v", snapshot_path)