My agentic slop goes here. Not intended for anyone else!

more tests

+50 -18
yaml/ocaml-yamle/bin/yamlcat.ml
···
Printf.eprintf "If no files are given, reads from stdin.\n";
Printf.eprintf "\n";
Printf.eprintf "Options:\n";
Printf.eprintf " --json Output as JSON format\n";
Printf.eprintf " --flow Output YAML in flow style\n";
Printf.eprintf " --debug Output internal representation (for debugging)\n";
···
json_to_string buf v;
Buffer.contents buf
-
let process_string ~format content =
try
-
match format with
-
| Yaml ->
-
let value = Yamle.of_string content in
-
print_string (Yamle.to_string value)
-
| Flow ->
-
let value = Yamle.of_string content in
-
print_string (Yamle.to_string ~layout_style:Yamle.Layout_style.Flow value)
-
| Json ->
-
let value = Yamle.of_string content in
-
print_endline (value_to_json value)
-
| Debug ->
-
let yaml = Yamle.yaml_of_string content in
-
Format.printf "%a@." Yamle.pp_yaml yaml
with
| Yamle.Yamle_error e ->
Printf.eprintf "Error: %s\n" (Yamle.Error.to_string e);
exit 1
-
let process_file ~format filename =
let content =
if filename = "-" then
In_channel.input_all In_channel.stdin
else
In_channel.with_open_text filename In_channel.input_all
in
-
process_string ~format content
let () =
let files = ref [] in
let format = ref Yaml in
let show_help = ref false in
(* Parse arguments *)
let args = Array.to_list Sys.argv |> List.tl in
List.iter (fun arg ->
match arg with
| "--help" | "-h" -> show_help := true
| "--json" -> format := Json
| "--flow" -> format := Flow
| "--debug" -> format := Debug
···
if files = [] then
(* Read from stdin *)
-
process_file ~format:!format "-"
else
-
List.iter (process_file ~format:!format) files
···
Printf.eprintf "If no files are given, reads from stdin.\n";
Printf.eprintf "\n";
Printf.eprintf "Options:\n";
+
Printf.eprintf " --all Output all documents (for multi-document YAML)\n";
Printf.eprintf " --json Output as JSON format\n";
Printf.eprintf " --flow Output YAML in flow style\n";
Printf.eprintf " --debug Output internal representation (for debugging)\n";
···
json_to_string buf v;
Buffer.contents buf
+
let process_string ~format ~all content =
try
+
if all then
+
(* Multi-document mode *)
+
match format with
+
| Yaml ->
+
let documents = Yamle.documents_of_string content in
+
print_string (Yamle.documents_to_string documents)
+
| Flow ->
+
let documents = Yamle.documents_of_string content in
+
print_string (Yamle.documents_to_string ~layout_style:Yamle.Layout_style.Flow documents)
+
| Json ->
+
let documents = Yamle.documents_of_string content in
+
let first = ref true in
+
List.iter (fun doc ->
+
match Yamle.Document.root doc with
+
| None -> ()
+
| Some yaml ->
+
if not !first then print_endline "---";
+
first := false;
+
let value = Yamle.to_json yaml in
+
print_endline (value_to_json value)
+
) documents
+
| Debug ->
+
let documents = Yamle.documents_of_string content in
+
List.iteri (fun i doc ->
+
Format.printf "Document %d:@." (i + 1);
+
Format.printf "%a@." Yamle.Document.pp doc
+
) documents
+
else
+
(* Single-document mode (original behavior) *)
+
match format with
+
| Yaml ->
+
let value = Yamle.of_string content in
+
print_string (Yamle.to_string value)
+
| Flow ->
+
let value = Yamle.of_string content in
+
print_string (Yamle.to_string ~layout_style:Yamle.Layout_style.Flow value)
+
| Json ->
+
let value = Yamle.of_string content in
+
print_endline (value_to_json value)
+
| Debug ->
+
let yaml = Yamle.yaml_of_string content in
+
Format.printf "%a@." Yamle.pp_yaml yaml
with
| Yamle.Yamle_error e ->
Printf.eprintf "Error: %s\n" (Yamle.Error.to_string e);
exit 1
+
let process_file ~format ~all filename =
let content =
if filename = "-" then
In_channel.input_all In_channel.stdin
else
In_channel.with_open_text filename In_channel.input_all
in
+
process_string ~format ~all content
let () =
let files = ref [] in
let format = ref Yaml in
let show_help = ref false in
+
let all = ref false in
(* Parse arguments *)
let args = Array.to_list Sys.argv |> List.tl in
List.iter (fun arg ->
match arg with
| "--help" | "-h" -> show_help := true
+
| "--all" -> all := true
| "--json" -> format := Json
| "--flow" -> format := Flow
| "--debug" -> format := Debug
···
if files = [] then
(* Read from stdin *)
+
process_file ~format:!format ~all:!all "-"
else
+
List.iter (process_file ~format:!format ~all:!all) files
+41 -8
yaml/ocaml-yamle/lib/emitter.ml
···
write_anchor t anchor;
write_tag t ~implicit tag;
if use_flow then begin
write_char t '[';
t.flow_level <- t.flow_level + 1;
t.need_separator <- false;
···
write_char t ']';
t.flow_level <- t.flow_level - 1;
t.need_separator <- true;
-
pop_state t
end else begin
t.indent <- t.indent - t.config.indent;
pop_state t
···
write_anchor t anchor;
write_tag t ~implicit tag;
if use_flow then begin
write_char t '{';
t.flow_level <- t.flow_level + 1;
t.need_separator <- false;
···
write_char t '}';
t.flow_level <- t.flow_level - 1;
t.need_separator <- true;
-
pop_state t
end else begin
t.indent <- t.indent - t.config.indent;
pop_state t
···
emit t (Event.Alias { anchor = name })
| `A seq ->
emit t (Event.Sequence_start {
anchor = Sequence.anchor seq;
tag = Sequence.tag seq;
implicit = Sequence.implicit seq;
-
style = Sequence.style seq;
});
-
List.iter (emit_yaml_node t) (Sequence.members seq);
emit t Event.Sequence_end
| `O map ->
emit t (Event.Mapping_start {
anchor = Mapping.anchor map;
tag = Mapping.tag map;
implicit = Mapping.implicit map;
-
style = Mapping.style map;
});
List.iter (fun (k, v) ->
emit_yaml_node t k;
emit_yaml_node t v
-
) (Mapping.members map);
emit t Event.Mapping_end
let emit_yaml t yaml =
···
| `A items ->
let style =
-
if t.config.layout_style = Layout_style.Flow then Layout_style.Flow
else Layout_style.Block
in
emit t (Event.Sequence_start {
···
| `O pairs ->
let style =
-
if t.config.layout_style = Layout_style.Flow then Layout_style.Flow
else Layout_style.Block
in
emit t (Event.Mapping_start {
···
let t = create ~config () in
emit_yaml t yaml;
contents t
···
write_anchor t anchor;
write_tag t ~implicit tag;
if use_flow then begin
+
write_char t ' ';
write_char t '[';
t.flow_level <- t.flow_level + 1;
t.need_separator <- false;
···
write_char t ']';
t.flow_level <- t.flow_level - 1;
t.need_separator <- true;
+
pop_state t;
+
(* Write newline if returning to block context *)
+
(match t.state with
+
| In_block_mapping_key _ | In_block_sequence _ -> write_newline t
+
| _ -> ())
end else begin
t.indent <- t.indent - t.config.indent;
pop_state t
···
write_anchor t anchor;
write_tag t ~implicit tag;
if use_flow then begin
+
write_char t ' ';
write_char t '{';
t.flow_level <- t.flow_level + 1;
t.need_separator <- false;
···
write_char t '}';
t.flow_level <- t.flow_level - 1;
t.need_separator <- true;
+
pop_state t;
+
(* Write newline if returning to block context *)
+
(match t.state with
+
| In_block_mapping_key _ | In_block_sequence _ -> write_newline t
+
| _ -> ())
end else begin
t.indent <- t.indent - t.config.indent;
pop_state t
···
emit t (Event.Alias { anchor = name })
| `A seq ->
+
let members = Sequence.members seq in
+
let style =
+
(* Force flow style for empty sequences *)
+
if members = [] then Layout_style.Flow
+
else Sequence.style seq
+
in
emit t (Event.Sequence_start {
anchor = Sequence.anchor seq;
tag = Sequence.tag seq;
implicit = Sequence.implicit seq;
+
style;
});
+
List.iter (emit_yaml_node t) members;
emit t Event.Sequence_end
| `O map ->
+
let members = Mapping.members map in
+
let style =
+
(* Force flow style for empty mappings *)
+
if members = [] then Layout_style.Flow
+
else Mapping.style map
+
in
emit t (Event.Mapping_start {
anchor = Mapping.anchor map;
tag = Mapping.tag map;
implicit = Mapping.implicit map;
+
style;
});
List.iter (fun (k, v) ->
emit_yaml_node t k;
emit_yaml_node t v
+
) members;
emit t Event.Mapping_end
let emit_yaml t yaml =
···
| `A items ->
let style =
+
(* Force flow style for empty sequences *)
+
if items = [] then Layout_style.Flow
+
else if t.config.layout_style = Layout_style.Flow then Layout_style.Flow
else Layout_style.Block
in
emit t (Event.Sequence_start {
···
| `O pairs ->
let style =
+
(* Force flow style for empty mappings *)
+
if pairs = [] then Layout_style.Flow
+
else if t.config.layout_style = Layout_style.Flow then Layout_style.Flow
else Layout_style.Block
in
emit t (Event.Mapping_start {
···
let t = create ~config () in
emit_yaml t yaml;
contents t
+
+
let documents_to_string ?(config = default_config) documents =
+
let t = create ~config () in
+
emit t (Event.Stream_start { encoding = config.encoding });
+
List.iter (emit_document t) documents;
+
emit t Event.Stream_end;
+
contents t
+6 -2
yaml/ocaml-yamle/lib/loader.ml
···
| [doc] ->
(match Document.root doc with
| None -> `Null
-
| Some yaml -> Yaml.to_value yaml)
| _ -> Error.raise Multiple_documents
(** Load single document as Yaml *)
···
state.documents <- [];
Some (match Document.root doc with
| None -> `Null
-
| Some yaml -> Yaml.to_value yaml)
| [] -> None)
| Event.Stream_end -> None
| _ -> loop ()
···
| [doc] ->
(match Document.root doc with
| None -> `Null
+
| Some yaml ->
+
let yaml = Yaml.resolve_aliases yaml in
+
Yaml.to_value yaml)
| _ -> Error.raise Multiple_documents
(** Load single document as Yaml *)
···
state.documents <- [];
Some (match Document.root doc with
| None -> `Null
+
| Some yaml ->
+
let yaml = Yaml.resolve_aliases yaml in
+
Yaml.to_value yaml)
| [] -> None)
| Event.Stream_end -> None
| _ -> loop ()
+3 -3
yaml/ocaml-yamle/lib/scanner.ml
···
(* Non-specific tag: ! *)
("!", "")
| Some '!' ->
-
(* Secondary handle *)
-
let handle = scan_tag_handle t in
let suffix = scan_tag_suffix t in
-
(handle, suffix)
| _ ->
(* Primary handle or just suffix *)
let first_part = scan_tag_suffix t in
···
(* Non-specific tag: ! *)
("!", "")
| Some '!' ->
+
(* Secondary handle: !! *)
+
ignore (Input.next t.input); (* consume second ! *)
let suffix = scan_tag_suffix t in
+
("!!", suffix)
| _ ->
(* Primary handle or just suffix *)
let first_part = scan_tag_suffix t in
+18 -1
yaml/ocaml-yamle/lib/yamle.ml
···
let of_string s = Loader.value_of_string s
(** {1 JSON-compatible emission} *)
let to_string
···
} in
Emitter.yaml_to_string ~config yaml
(** {1 Conversion} *)
-
let to_json yaml = Yaml.to_value yaml
let of_json value = Yaml.of_value value
···
let of_string s = Loader.value_of_string s
+
let documents_of_string s = Loader.documents_of_string s
+
(** {1 JSON-compatible emission} *)
let to_string
···
} in
Emitter.yaml_to_string ~config yaml
+
let documents_to_string
+
?(encoding = Encoding.Utf8)
+
?(scalar_style = Scalar_style.Any)
+
?(layout_style = Layout_style.Any)
+
documents =
+
let config = {
+
Emitter.default_config with
+
encoding;
+
scalar_style;
+
layout_style;
+
} in
+
Emitter.documents_to_string ~config documents
+
(** {1 Conversion} *)
+
let to_json yaml =
+
let yaml = Yaml.resolve_aliases yaml in
+
Yaml.to_value yaml
let of_json value = Yaml.of_value value
+22 -18
yaml/ocaml-yamle/tests/cram/collections.t
···
config:
concurrency: 10
empty_collections:
-
empty_sequence:
-
empty_mapping:
sequence_with_empty:
- value1
-
-
- value2
mapping_with_empty:
key1: value1
-
key2:
key3: value3
Test collections_block.yml with JSON output
···
- design
compact_with_empty:
- id: 1
-
data:
-
meta:
- id: 2
data:
- item1
···
- path: '/users/:id'
method: GET
auth: required
-
params:
- path: /users
method: POST
auth: required
···
- b
- c
empty_flow:
-
empty_seq:
-
empty_map:
both:
-
-
-
- flow_in_block:
sequence:
- 1
- 2
···
single_item_map:
only: one
nested_empty:
-
-
-
- -
-
- -
all_empty:
-
- -
-
- a:
-
- b:
Test collections_flow.yml with JSON output
···
$ echo 'empty_seq: []
> empty_map: {}' | yamlcat
-
empty_seq:
-
empty_map:
$ echo 'empty_seq: []
> empty_map: {}' | yamlcat --json
···
config:
concurrency: 10
empty_collections:
+
empty_sequence: []
+
empty_mapping: {}
sequence_with_empty:
- value1
+
- []
- value2
mapping_with_empty:
key1: value1
+
key2: {}
key3: value3
Test collections_block.yml with JSON output
···
- design
compact_with_empty:
- id: 1
+
data: []
+
meta: {}
- id: 2
data:
- item1
···
- path: '/users/:id'
method: GET
auth: required
+
params: []
- path: /users
method: POST
auth: required
···
- b
- c
empty_flow:
+
empty_seq: []
+
empty_map: {}
both:
+
- []
+
- {}
+
flow_in_block:
sequence:
- 1
- 2
···
single_item_map:
only: one
nested_empty:
+
- []
-
+
- {}
-
+
- {}
+
- []
all_empty:
+
- {}
+
- []
+
- a: []
+
- b: {}
Test collections_flow.yml with JSON output
···
$ echo 'empty_seq: []
> empty_map: {}' | yamlcat
+
empty_seq: []
+
empty_map: {}
$ echo 'empty_seq: []
> empty_map: {}' | yamlcat --json
+26 -14
yaml/ocaml-yamle/tests/cram/failing_anchors.t
···
$ echo 'anchor: &anc value
> alias: *anc' | yamlcat 2>&1
-
Error: unresolved alias: *anc
-
[1]
Test: Numeric anchor and alias
$ echo 'original: &num 42
> copy: *num' | yamlcat 2>&1
-
Error: unresolved alias: *num
-
[1]
Test: Sequence anchor and alias
···
> - one
> - two
> copy: *items' | yamlcat 2>&1
-
Error: unresolved alias: *items
-
[1]
Test: Mapping anchor and alias
···
> name: Alice
> age: 30
> copy: *p' | yamlcat 2>&1
-
Error: unresolved alias: *p
-
[1]
Test: Multiple aliases to same anchor
···
> first: *v
> second: *v
> third: *v' | yamlcat 2>&1
-
Error: unresolved alias: *v
-
[1]
Test: Anchor in flow context
$ echo '[&item apple, *item]' | yamlcat 2>&1
-
Error: unresolved alias: *item
-
[1]
Test: Anchor with mapping in flow
$ echo '{original: &cfg {a: 1}, copy: *cfg}' | yamlcat 2>&1
-
Error: unresolved alias: *cfg
-
[1]
Test: Anchors file from test suite
···
$ echo 'anchor: &anc value
> alias: *anc' | yamlcat 2>&1
+
anchor: value
+
alias: value
Test: Numeric anchor and alias
$ echo 'original: &num 42
> copy: *num' | yamlcat 2>&1
+
original: 42
+
copy: 42
Test: Sequence anchor and alias
···
> - one
> - two
> copy: *items' | yamlcat 2>&1
+
list:
+
- one
+
- two
+
copy:
+
- one
+
- two
Test: Mapping anchor and alias
···
> name: Alice
> age: 30
> copy: *p' | yamlcat 2>&1
+
person:
+
name: Alice
+
age: 30
+
copy:
+
name: Alice
+
age: 30
Test: Multiple aliases to same anchor
···
> first: *v
> second: *v
> third: *v' | yamlcat 2>&1
+
value: 100
+
first: 100
+
second: 100
+
third: 100
Test: Anchor in flow context
$ echo '[&item apple, *item]' | yamlcat 2>&1
+
- apple
+
- apple
Test: Anchor with mapping in flow
$ echo '{original: &cfg {a: 1}, copy: *cfg}' | yamlcat 2>&1
+
original:
+
a: 1
+
copy:
+
a: 1
Test: Anchors file from test suite
+7 -7
yaml/ocaml-yamle/tests/cram/failing_empty.t
···
Test: Empty sequence emits as nothing instead of []
$ echo 'empty_seq: []' | yamlcat
-
empty_seq:
The above outputs just "empty_seq:" with nothing after it.
Expected output should be: empty_seq: []
···
Test: Empty mapping emits as nothing instead of {}
$ echo 'empty_map: {}' | yamlcat
-
empty_map:
Test: Multiple empty collections
$ echo 'seq: []
> map: {}
> data: value' | yamlcat
-
seq:
-
map:
data: value
Test: Nested empty collections
···
> inner_seq: []
> inner_map: {}' | yamlcat
outer:
-
inner_seq:
-
inner_map:
Test: Empty collection in sequence
···
> - third' | yamlcat
items:
- first
-
-
- third
Test: Verify JSON output is correct (for comparison)
···
Test: Empty sequence emits as nothing instead of []
$ echo 'empty_seq: []' | yamlcat
+
empty_seq: []
The above outputs just "empty_seq:" with nothing after it.
Expected output should be: empty_seq: []
···
Test: Empty mapping emits as nothing instead of {}
$ echo 'empty_map: {}' | yamlcat
+
empty_map: {}
Test: Multiple empty collections
$ echo 'seq: []
> map: {}
> data: value' | yamlcat
+
seq: []
+
map: {}
data: value
Test: Nested empty collections
···
> inner_seq: []
> inner_map: {}' | yamlcat
outer:
+
inner_seq: []
+
inner_map: {}
Test: Empty collection in sequence
···
> - third' | yamlcat
items:
- first
+
- []
- third
Test: Verify JSON output is correct (for comparison)
+6 -12
yaml/ocaml-yamle/tests/cram/failing_tags.t
···
Test: String tag shorthand
$ echo '!!str 123' | yamlcat
-
Error: invalid tag: !str
-
[1]
Expected: 123 (as a string)
Test: Integer tag shorthand
$ echo '!!int "42"' | yamlcat
-
Error: invalid tag: !int
-
[1]
Expected: 42
Test: Boolean tag shorthand
$ echo '!!bool "yes"' | yamlcat
-
Error: invalid tag: !bool
-
[1]
Expected: true
Test: Null tag shorthand
$ echo '!!null ""' | yamlcat
-
Error: invalid tag: !null
-
[1]
Expected: null
Test: Float tag shorthand
$ echo '!!float 3.14' | yamlcat
-
Error: invalid tag: !float
-
[1]
Expected: 3.14
Test: Tag shorthand in mapping value
$ echo 'value: !!str 42' | yamlcat
-
Error: invalid tag: !str
-
[1]
Expected: value: "42" (string representation)
···
Test: String tag shorthand
$ echo '!!str 123' | yamlcat
+
'123'
Expected: 123 (as a string)
Test: Integer tag shorthand
$ echo '!!int "42"' | yamlcat
+
42
Expected: 42
Test: Boolean tag shorthand
$ echo '!!bool "yes"' | yamlcat
+
true
Expected: true
Test: Null tag shorthand
$ echo '!!null ""' | yamlcat
+
null
Expected: null
Test: Float tag shorthand
$ echo '!!float 3.14' | yamlcat
+
3.14
Expected: 3.14
Test: Tag shorthand in mapping value
$ echo 'value: !!str 42' | yamlcat
+
value: '42'
Expected: value: "42" (string representation)