<commit_before>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import dedent from '../../jsutils/dedent';
import { printSchema, printIntrospectionSchema } from '../schemaPrinter';
import { buildSchema } from '../buildASTSchema';
import {
  GraphQLSchema,
  GraphQLInputObjectType,
  GraphQLScalarType,
  GraphQLObjectType,
  GraphQLInterfaceType,
  GraphQLUnionType,
  GraphQLEnumType,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean,
  GraphQLList,
  GraphQLNonNull,
} from '../../';
import { GraphQLDirective } from '../../type/directives';
import { DirectiveLocation } from '../../language/directiveLocation';

function printForTest(schema) {
  const schemaText = printSchema(schema);
  // keep printSchema and buildSchema in sync
  expect(printSchema(buildSchema(schemaText))).to.equal(schemaText);
  return schemaText;
}

function printSingleFieldSchema(fieldConfig) {
  const Root = new GraphQLObjectType({
    name: 'Root',
    fields: { singleField: fieldConfig },
  });
  return printForTest(new GraphQLSchema({ query: Root }));
}

function listOf(type) {
  return GraphQLList(type);
}

function nonNull(type) {
  return GraphQLNonNull(type);
}

describe('Type System Printer', () => {
  it('Prints String Field', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: String
      }
    `);
  });

  it('Prints [String] Field', () => {
    const output = printSingleFieldSchema({
      type: listOf(GraphQLString),
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: [String]
      }
    `);
  });

  it('Prints String! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(GraphQLString),
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: String!
      }
    `);
  });

  it('Prints [String]! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(listOf(GraphQLString)),
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: [String]!
      }
    `);
  });

  it('Prints [String!] Field', () => {
    const output = printSingleFieldSchema({
      type: listOf(nonNull(GraphQLString)),
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: [String!]
      }
    `);
  });

  it('Prints [String!]! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(listOf(nonNull(GraphQLString))),
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField: [String!]!
      }
    `);
  });

  it('Print Object Field', () => {
    const FooType = new GraphQLObjectType({
      name: 'Foo',
      fields: { str: { type: GraphQLString } },
    });

    const Root = new GraphQLObjectType({
      name: 'Root',
      fields: { foo: { type: FooType } },
    });

    const Schema = new GraphQLSchema({ query: Root });
    const output = printForTest(Schema);
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Foo {
        str: String
      }

      type Root {
        foo: Foo
      }
    `);
  });

  it('Prints String Field With Int Arg', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt } },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int): String
      }
    `);
  });

  it('Prints String Field With Int Arg With Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt, defaultValue: 2 } },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int = 2): String
      }
    `);
  });

  it('Prints String Field With String Arg With Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLString, defaultValue: 'tes\t de\fault' } },
    });
    expect(output).to.equal(
      dedent(String.raw`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: String = "tes\t de\fault"): String
      }
    `),
    );
  });

  it('Prints String Field With Int Arg With Default Null', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt, defaultValue: null } },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int = null): String
      }
    `);
  });

  it('Prints String Field With Int! Arg', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: nonNull(GraphQLInt) } },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int!): String
      }
    `);
  });

  it('Prints String Field With Multiple Args', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString },
      },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int, argTwo: String): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, First is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt, defaultValue: 1 },
        argTwo: { type: GraphQLString },
        argThree: { type: GraphQLBoolean },
      },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, Second is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString, defaultValue: 'foo' },
        argThree: { type: GraphQLBoolean },
      },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, Last is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString },
        argThree: { type: GraphQLBoolean, defaultValue: false },
      },
    });
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Root {
        singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String
      }
    `);
  });

  it('Print Interface', () => {
    const FooType = new GraphQLInterfaceType({
      name: 'Foo',
      fields: { str: { type: GraphQLString } },<commit_msg>Remove redundancy in 'schemaPrinter' tests (#1211)<commit_after>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import dedent from '../../jsutils/dedent';
import { printSchema, printIntrospectionSchema } from '../schemaPrinter';
import { buildSchema } from '../buildASTSchema';
import {
  GraphQLSchema,
  GraphQLInputObjectType,
  GraphQLScalarType,
  GraphQLObjectType,
  GraphQLInterfaceType,
  GraphQLUnionType,
  GraphQLEnumType,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean,
  GraphQLList,
  GraphQLNonNull,
} from '../../';
import { GraphQLDirective } from '../../type/directives';
import { DirectiveLocation } from '../../language/directiveLocation';

function printForTest(schema) {
  const schemaText = printSchema(schema);
  // keep printSchema and buildSchema in sync
  expect(printSchema(buildSchema(schemaText))).to.equal(schemaText);
  return schemaText;
}

function printSingleFieldSchema(fieldConfig) {
  const Query = new GraphQLObjectType({
    name: 'Query',
    fields: { singleField: fieldConfig },
  });
  return printForTest(new GraphQLSchema({ query: Query }));
}

function listOf(type) {
  return GraphQLList(type);
}

function nonNull(type) {
  return GraphQLNonNull(type);
}

describe('Type System Printer', () => {
  it('Prints String Field', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: String
      }
    `);
  });

  it('Prints [String] Field', () => {
    const output = printSingleFieldSchema({
      type: listOf(GraphQLString),
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: [String]
      }
    `);
  });

  it('Prints String! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(GraphQLString),
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: String!
      }
    `);
  });

  it('Prints [String]! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(listOf(GraphQLString)),
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: [String]!
      }
    `);
  });

  it('Prints [String!] Field', () => {
    const output = printSingleFieldSchema({
      type: listOf(nonNull(GraphQLString)),
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: [String!]
      }
    `);
  });

  it('Prints [String!]! Field', () => {
    const output = printSingleFieldSchema({
      type: nonNull(listOf(nonNull(GraphQLString))),
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField: [String!]!
      }
    `);
  });

  it('Print Object Field', () => {
    const FooType = new GraphQLObjectType({
      name: 'Foo',
      fields: { str: { type: GraphQLString } },
    });

    const Root = new GraphQLObjectType({
      name: 'Root',
      fields: { foo: { type: FooType } },
    });

    const Schema = new GraphQLSchema({ query: Root });
    const output = printForTest(Schema);
    expect(output).to.equal(dedent`
      schema {
        query: Root
      }

      type Foo {
        str: String
      }

      type Root {
        foo: Foo
      }
    `);
  });

  it('Prints String Field With Int Arg', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt } },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int): String
      }
    `);
  });

  it('Prints String Field With Int Arg With Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt, defaultValue: 2 } },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int = 2): String
      }
    `);
  });

  it('Prints String Field With String Arg With Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLString, defaultValue: 'tes\t de\fault' } },
    });
    expect(output).to.equal(
      dedent(String.raw`
      type Query {
        singleField(argOne: String = "tes\t de\fault"): String
      }
    `),
    );
  });

  it('Prints String Field With Int Arg With Default Null', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: GraphQLInt, defaultValue: null } },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int = null): String
      }
    `);
  });

  it('Prints String Field With Int! Arg', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: { argOne: { type: nonNull(GraphQLInt) } },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int!): String
      }
    `);
  });

  it('Prints String Field With Multiple Args', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString },
      },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int, argTwo: String): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, First is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt, defaultValue: 1 },
        argTwo: { type: GraphQLString },
        argThree: { type: GraphQLBoolean },
      },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, Second is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString, defaultValue: 'foo' },
        argThree: { type: GraphQLBoolean },
      },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String
      }
    `);
  });

  it('Prints String Field With Multiple Args, Last is Default', () => {
    const output = printSingleFieldSchema({
      type: GraphQLString,
      args: {
        argOne: { type: GraphQLInt },
        argTwo: { type: GraphQLString },
        argThree: { type: GraphQLBoolean, defaultValue: false },
      },
    });
    expect(output).to.equal(dedent`
      type Query {
        singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String
      }
    `);
  });

  it('Print Interface', () => {
    const FooType = new GraphQLInterfaceType({
      name: 'Foo',
      fields: { str: { type: GraphQLString } },
{'fraction_non_alphanumeric': 0.1389324425659554, 'fraction_numerical': 0.0013634194559956371, 'mean_word_length': 2.438818565400844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7478314', 'n_tokens_mistral': 4549, 'n_tokens_neox': 4465, 'n_words': 1066}