Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for Serde #137

Closed
wants to merge 4 commits into from
Closed

Added tests for Serde #137

wants to merge 4 commits into from

Conversation

h7kanna
Copy link
Contributor

@h7kanna h7kanna commented Sep 17, 2024

This is follow up for PR #135
Without jaq tests are becoming verbose and unwieldy.
I hope adding a test dependency is fine with you.

@wooorm
Copy link
Owner

wooorm commented Sep 19, 2024

Thanks! Hmm. I am not sure whether I want tests that only look for a type field?
What was verbose and unwieldy?
Testing the generated JSON doesn’t have to be too complex?

@h7kanna
Copy link
Contributor Author

h7kanna commented Sep 19, 2024

Thanks! Hmm. I am not sure whether I want tests that only look for a type field? What was verbose and unwieldy? Testing the generated JSON doesn’t have to be too complex?

Not complex, very verbose and unreadable tests. I can add asserts for all the properties not only for the type if you are ok with the approach. But by directly testing JSON, most of the content of the fixture is spans (which I think redundant to check for serde).

For example

assert_jq("a", ".children[0].type", "paragraph", None)
assert_jq("a", ".children[0].children[0].value", "a", None)

vs

assert_eq!(r#"{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "a",
          "position": {
            "start": {
              "line": 1,
              "column": 1,
              "offset": 0
            },
            "end": {
              "line": 1,
              "column": 2,
              "offset": 1
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 1,
          "column": 2,
          "offset": 1
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 1,
      "column": 2,
      "offset": 1
    }
  }
}"#, actual_serde_json)

@wooorm
Copy link
Owner

wooorm commented Sep 19, 2024

If you remove positional info and indentation it becomes fine?

@h7kanna
Copy link
Contributor Author

h7kanna commented Sep 19, 2024

If you remove positional info and indentation it becomes fine?

Do you mean like below? But positions are not optional in the parsed mdast even though we can make them optional in serde using #[serde(skip_serializing_if = "Option::is_none")].

assert_eq!(r#"{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "a",
        }
      ]
    }
  ]
}"#, actual_serde_json)

@wooorm
Copy link
Owner

wooorm commented Sep 19, 2024

What I was thinking of is:

assert_eq!(r#"{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"a"}]}]}"#, actual_serde_json)

Or, isn’t it actual first, then expected?:

assert_eq!(actual_serde_json, r#"{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"a"}]}]}"#)

You can also choose to dig into a node of course: we often don’t care about root:

assert_eq!(node, r#"{"type":"paragraph","children":[{"type":"text","value":"a"}]}"#)

@h7kanna
Copy link
Contributor Author

h7kanna commented Sep 19, 2024

What I was thinking of is:

assert_eq!(r#"{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"a"}]}]}"#, actual_serde_json)

Or, isn’t it actual first, then expected?:

assert_eq!(actual_serde_json, r#"{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"a"}]}]}"#)

You can also choose to dig into a node of course: we often don’t care about root:

assert_eq!(node, r#"{"type":"paragraph","children":[{"type":"text","value":"a"}]}"#)

I mean we have to come up with the implementation of that assert_eq which I did using jql.
Direct JSON(expected) comparison without the positional info is not possible as parsed MDAST(actual) will have the positions.

@wooorm
Copy link
Owner

wooorm commented Sep 19, 2024

Direct JSON(expected) comparison without the positional info is not possible as parsed MDAST(actual) will have the positions.

I believe that the comparison is possible after using a utility function?

This is the JavaScript version: https://github.com/syntax-tree/unist-util-remove-position. Walking trees is a bit different in Rust, but the approach should be similar?

@h7kanna
Copy link
Contributor Author

h7kanna commented Sep 19, 2024

Direct JSON(expected) comparison without the positional info is not possible as parsed MDAST(actual) will have the positions.

I believe that the comparison is possible after using a utility function?

Yes

This is the JavaScript version: https://github.com/syntax-tree/unist-util-remove-position. Walking trees is a bit different in Rust, but the approach should be similar?

I will try to implement visit on Node.

Though, It's just 'query the result JSON' versus 'modify the result(visit) to match'.

@h7kanna
Copy link
Contributor Author

h7kanna commented Sep 19, 2024

Closing this one as there two many changes.
Follow up with requested changes here #140

@h7kanna h7kanna closed this Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants