Use multi-line block for trailing quote

ref: fdc10bb918 (diff-ebaed8492e8d884ee4f2255e39909568)
This commit is contained in:
Daniel Tschinder 2018-06-25 14:26:53 +02:00
parent a032367e26
commit 6e64983f82
2 changed files with 117 additions and 80 deletions

View File

@ -275,15 +275,35 @@ class SchemaPrinter
return self::printDescriptionWithComments($lines, $indentation, $firstInBlock); return self::printDescriptionWithComments($lines, $indentation, $firstInBlock);
} }
$description = ($indentation && !$firstInBlock) ? "\n" : ''; $description = ($indentation && !$firstInBlock)
if (count($lines) === 1 && mb_strlen($lines[0]) < 70) { ? "\n" . $indentation . '"""'
$description .= $indentation . '"""' . self::escapeQuote($lines[0]) . "\"\"\"\n"; : $indentation . '"""';
return $description;
// In some circumstances, a single line can be used for the description.
if (
count($lines) === 1 &&
mb_strlen($lines[0]) < 70 &&
substr($lines[0], -1) !== '"'
) {
return $description . self::escapeQuote($lines[0]) . "\"\"\"\n";
} }
$description .= $indentation . "\"\"\"\n"; // Format a multi-line block quote to account for leading space.
foreach ($lines as $line) { $hasLeadingSpace = isset($lines[0]) &&
$description .= $indentation . self::escapeQuote($line) . "\n"; (
substr($lines[0], 0, 1) === ' ' ||
substr($lines[0], 0, 1) === '\t'
);
if (!$hasLeadingSpace) {
$description .= "\n";
}
$lineLength = count($lines);
for ($i = 0; $i < $lineLength; $i++) {
if ($i !== 0 || !$hasLeadingSpace) {
$description .= $indentation;
}
$description .= self::escapeQuote($lines[$i]) . "\n";
} }
$description .= $indentation . "\"\"\"\n"; $description .= $indentation . "\"\"\"\n";

View File

@ -11,6 +11,7 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Definition\UnionType;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\SchemaPrinter; use GraphQL\Utils\SchemaPrinter;
class SchemaPrinterTest extends \PHPUnit_Framework_TestCase class SchemaPrinterTest extends \PHPUnit_Framework_TestCase
@ -24,13 +25,13 @@ class SchemaPrinterTest extends \PHPUnit_Framework_TestCase
private function printSingleFieldSchema($fieldConfig) private function printSingleFieldSchema($fieldConfig)
{ {
$root = new ObjectType([ $query = new ObjectType([
'name' => 'Root', 'name' => 'Query',
'fields' => [ 'fields' => [
'singleField' => $fieldConfig 'singleField' => $fieldConfig
] ]
]); ]);
return $this->printForTest(new Schema(['query' => $root])); return $this->printForTest(new Schema(['query' => $query]));
} }
/** /**
@ -42,11 +43,7 @@ class SchemaPrinterTest extends \PHPUnit_Framework_TestCase
'type' => Type::string() 'type' => Type::string()
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: String singleField: String
} }
', $output); ', $output);
@ -61,11 +58,7 @@ type Root {
'type' => Type::listOf(Type::string()) 'type' => Type::listOf(Type::string())
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: [String] singleField: [String]
} }
', $output); ', $output);
@ -80,11 +73,7 @@ type Root {
'type' => Type::nonNull(Type::string()) 'type' => Type::nonNull(Type::string())
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: String! singleField: String!
} }
', $output); ', $output);
@ -99,11 +88,7 @@ type Root {
'type' => Type::nonNull(Type::listOf(Type::string())) 'type' => Type::nonNull(Type::listOf(Type::string()))
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: [String]! singleField: [String]!
} }
', $output); ', $output);
@ -118,11 +103,7 @@ type Root {
'type' => Type::listOf(Type::nonNull(Type::string())) 'type' => Type::listOf(Type::nonNull(Type::string()))
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: [String!] singleField: [String!]
} }
', $output); ', $output);
@ -137,11 +118,7 @@ type Root {
'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string()))) 'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string())))
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField: [String!]! singleField: [String!]!
} }
', $output); ', $output);
@ -189,11 +166,7 @@ type Root {
'args' => ['argOne' => ['type' => Type::int()]] 'args' => ['argOne' => ['type' => Type::int()]]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int): String singleField(argOne: Int): String
} }
', $output); ', $output);
@ -209,11 +182,7 @@ type Root {
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]] 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int = 2): String singleField(argOne: Int = 2): String
} }
', $output); ', $output);
@ -229,11 +198,7 @@ type Root {
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]] 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int = null): String singleField(argOne: Int = null): String
} }
', $output); ', $output);
@ -249,11 +214,7 @@ type Root {
'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]] 'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int!): String singleField(argOne: Int!): String
} }
', $output); ', $output);
@ -272,11 +233,7 @@ type Root {
] ]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int, argTwo: String): String singleField(argOne: Int, argTwo: String): String
} }
', $output); ', $output);
@ -296,11 +253,7 @@ type Root {
] ]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String
} }
', $output); ', $output);
@ -320,11 +273,7 @@ type Root {
] ]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String
} }
', $output); ', $output);
@ -344,11 +293,7 @@ type Root {
] ]
]); ]);
$this->assertEquals(' $this->assertEquals('
schema { type Query {
query: Root
}
type Root {
singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String
} }
', $output); ', $output);
@ -661,6 +606,78 @@ type Query {
', $output); ', $output);
} }
/**
* @it One-line prints a short description
*/
public function testOneLinePrintsAShortDescription()
{
$description = 'This field is awesome';
$output = $this->printSingleFieldSchema([
"type" => Type::string(),
"description" => $description
]);
$this->assertEquals('
type Query {
"""This field is awesome"""
singleField: String
}
', $output);
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
$recreatedField = $recreatedRoot->getFields()['singleField'];
$this->assertEquals($description, $recreatedField->description);
}
/**
* @it Does not one-line print a description that ends with a quote
*/
public function testDoesNotOneLinePrintADescriptionThatEndsWithAQuote()
{
$description = 'This field is "awesome"';
$output = $this->printSingleFieldSchema([
"type" => Type::string(),
"description" => $description
]);
$this->assertEquals('
type Query {
"""
This field is "awesome"
"""
singleField: String
}
', $output);
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
$recreatedField = $recreatedRoot->getFields()['singleField'];
$this->assertEquals($description, $recreatedField->description);
}
/**
* @it Preserves leading spaces when printing a description
*/
public function testPReservesLeadingSpacesWhenPrintingADescription()
{
$description = ' This field is "awesome"';
$output = $this->printSingleFieldSchema([
"type" => Type::string(),
"description" => $description
]);
$this->assertEquals('
type Query {
""" This field is "awesome"
"""
singleField: String
}
', $output);
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
$recreatedField = $recreatedRoot->getFields()['singleField'];
$this->assertEquals($description, $recreatedField->description);
}
/** /**
* @it Print Introspection Schema * @it Print Introspection Schema
*/ */