<commit_before>package sangria.renderer

import org.scalatest.{Matchers, WordSpec}
import sangria.ast.{Directive, Field, AstNode}
import sangria.parser.QueryParser
import sangria.util.FileUtil
import sangria.macros._

import scala.util.Success

class QueryRendererSpec extends WordSpec with Matchers {
  "QueryRenderer" should {
    "render kitchen sink" in {
      val Success(ast) = QueryParser.parse(FileUtil loadQuery "kitchen-sink.graphql")

      val prettyRendered = QueryRenderer.render(ast, QueryRenderer.Pretty)
      val compactRendered = QueryRenderer.render(ast, QueryRenderer.Compact)

      val Success(prettyParsed) = QueryParser.parse(prettyRendered)
      val Success(compactParsed) = QueryParser.parse(compactRendered)

      AstNode.withoutPosition(ast) should be (AstNode.withoutPosition(prettyParsed))
      AstNode.withoutPosition(ast) should be (AstNode.withoutPosition(compactParsed))

      compactRendered should be (
        "query queryName($foo:ComplexType,$site:Site=MOBILE){whoever123is:node(id:[123,456]){" +
            "id ... on User@defer{field2{id alias:field1(first:10,after:$foo)@include(if:$foo){id ...frag}}}}}" +
            "mutation likeStory{like(story:123)@defer{story{id}}}" +
            "subscription StoryLikeSubscription($input:StoryLikeSubscribeInput){storyLikeSubscribe(input:$input){story{likers{count} likeSentence{text}}}}" +
            "fragment frag on Friend{foo(size:$size,bar:$b,obj:" +
            "{key:\"value\"})}{unnamed(truthy:true,falsey:false) query ... @skip(unless:$foo){id} ... {id}}")

      prettyRendered should be (
        """query queryName($foo: ComplexType, $site: Site = MOBILE) {
         |  whoever123is: node(id: [123, 456]) {
         |    id
         |    ... on User @defer {
         |      field2 {
         |        id
         |        alias: field1(first: 10, after: $foo) @include(if: $foo) {
         |          id
         |          ...frag
         |        }
         |      }
         |    }
         |  }
         |}
         |
         |mutation likeStory {
         |  like(story: 123) @defer {
         |    story {
         |      id
         |    }
         |  }
         |}
         |
         |subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
         |  storyLikeSubscribe(input: $input) {
         |    story {
         |      likers {
         |        count
         |      }
         |      likeSentence {
         |        text
         |      }
         |    }
         |  }
         |}
         |
         |fragment frag on Friend {
         |  foo(size: $size, bar: $b, obj: {key: "value"})
         |}
         |
         |{
         |  unnamed(truthy: true, falsey: false)
         |  query
         |  ...  @skip(unless: $foo) {
         |    id
         |  }
         |  ...  {
         |    id
         |  }
         |}""".stripMargin)
    }

    "render partial AST" in {
      val ast = Field(Some("al"), "field1", Nil, List(Directive("foo", Nil)), Nil)

      QueryRenderer.render(ast) should be ("al: field1 @foo")
    }

    "correctly prints query operations without name" in {
      val ast = graphql"query { id, name }"

      QueryRenderer.render(ast).replaceAll("\r", "") should be (
        """{
          |  id
          |  name
          |}""".stripMargin.replaceAll("\r", ""))
    }

    "correctly prints query operations with artifacts and without name" in {
      val ast = graphql"query ($$foo: TestType) @testDirective { id, name }"

      QueryRenderer.render(ast).replaceAll("\r", "") should be (
        """query ($foo: TestType) @testDirective {
          |  id
          |  name
          |}""".stripMargin.replaceAll("\r", ""))
    }

    "correctly prints mutation operations with artifacts and without name" in {
      val ast = graphql"mutation ($$foo: TestType) @testDirective { id, name }"

      QueryRenderer.render(ast).replaceAll("\r", "") should be (
        """mutation ($foo: TestType) @testDirective {
          |  id
          |  name
          |}""".stripMargin.replaceAll("\r", ""))
    }
  }
}<commit_msg>Use a Scalactic string normalizer to simplify tests<commit_after>package sangria.renderer

import org.scalactic._
import org.scalatest.{Matchers, WordSpec}
import sangria.ast.{Directive, Field, AstNode}
import sangria.parser.QueryParser
import sangria.util.FileUtil
import sangria.macros._

import scala.util.Success

class QueryRendererSpec extends WordSpec with Matchers {

  def strippedOfCarriageReturns = new AbstractStringUniformity {
    def normalized(s: String): String = s.replaceAll("\\r", "")
  }

  "QueryRenderer" should {
    "render kitchen sink" in {
      val Success(ast) = QueryParser.parse(FileUtil loadQuery "kitchen-sink.graphql")

      val prettyRendered = QueryRenderer.render(ast, QueryRenderer.Pretty)
      val compactRendered = QueryRenderer.render(ast, QueryRenderer.Compact)

      val Success(prettyParsed) = QueryParser.parse(prettyRendered)
      val Success(compactParsed) = QueryParser.parse(compactRendered)

      AstNode.withoutPosition(ast) should be (AstNode.withoutPosition(prettyParsed))
      AstNode.withoutPosition(ast) should be (AstNode.withoutPosition(compactParsed))

      compactRendered should be (
        "query queryName($foo:ComplexType,$site:Site=MOBILE){whoever123is:node(id:[123,456]){" +
            "id ... on User@defer{field2{id alias:field1(first:10,after:$foo)@include(if:$foo){id ...frag}}}}}" +
            "mutation likeStory{like(story:123)@defer{story{id}}}" +
            "subscription StoryLikeSubscription($input:StoryLikeSubscribeInput){storyLikeSubscribe(input:$input){story{likers{count} likeSentence{text}}}}" +
            "fragment frag on Friend{foo(size:$size,bar:$b,obj:" +
            "{key:\"value\"})}{unnamed(truthy:true,falsey:false) query ... @skip(unless:$foo){id} ... {id}}")

      prettyRendered should be (
        """query queryName($foo: ComplexType, $site: Site = MOBILE) {
         |  whoever123is: node(id: [123, 456]) {
         |    id
         |    ... on User @defer {
         |      field2 {
         |        id
         |        alias: field1(first: 10, after: $foo) @include(if: $foo) {
         |          id
         |          ...frag
         |        }
         |      }
         |    }
         |  }
         |}
         |
         |mutation likeStory {
         |  like(story: 123) @defer {
         |    story {
         |      id
         |    }
         |  }
         |}
         |
         |subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
         |  storyLikeSubscribe(input: $input) {
         |    story {
         |      likers {
         |        count
         |      }
         |      likeSentence {
         |        text
         |      }
         |    }
         |  }
         |}
         |
         |fragment frag on Friend {
         |  foo(size: $size, bar: $b, obj: {key: "value"})
         |}
         |
         |{
         |  unnamed(truthy: true, falsey: false)
         |  query
         |  ...  @skip(unless: $foo) {
         |    id
         |  }
         |  ...  {
         |    id
         |  }
         |}""".stripMargin)
    }

    "render partial AST" in {
      val ast = Field(Some("al"), "field1", Nil, List(Directive("foo", Nil)), Nil)

      QueryRenderer.render(ast) should be ("al: field1 @foo")
    }

    "correctly prints query operations without name" in {
      val ast = graphql"query { id, name }"

      QueryRenderer.render(ast) should equal (
        """{
          |  id
          |  name
          |}""".stripMargin) (after being strippedOfCarriageReturns)
    }

    "correctly prints query operations with artifacts and without name" in {
      val ast = graphql"query ($$foo: TestType) @testDirective { id, name }"

      QueryRenderer.render(ast) should equal (
        """query ($foo: TestType) @testDirective {
          |  id
          |  name
          |}""".stripMargin) (after being strippedOfCarriageReturns)
    }

    "correctly prints mutation operations with artifacts and without name" in {
      val ast = graphql"mutation ($$foo: TestType) @testDirective { id, name }"

      QueryRenderer.render(ast) should equal (
        """mutation ($foo: TestType) @testDirective {
          |  id
          |  name
          |}""".stripMargin) (after being strippedOfCarriageReturns)
    }
  }
}
{'fraction_non_alphanumeric': 0.13315833432764587, 'fraction_numerical': 0.008113590263691683, 'mean_word_length': 2.0238095238095237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '823979', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2481, 'n_words': 501}