2014-12-27 23:29:03 -07:00
|
|
|
Beautify = require '../lib/beautify'
|
|
|
|
beautifier = require "../lib/language-options"
|
|
|
|
languages = beautifier.languages
|
|
|
|
defaultLanguageOptions = beautifier.defaultLanguageOptions
|
|
|
|
fs = require "fs"
|
|
|
|
path = require "path"
|
2014-12-28 00:23:05 -07:00
|
|
|
options = require "../lib/options"
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
|
|
|
|
#
|
|
|
|
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
|
|
|
|
# or `fdescribe`). Remove the `f` to unfocus the block.
|
|
|
|
|
|
|
|
describe "BeautifyLanguages", ->
|
|
|
|
|
|
|
|
optionsDir = path.resolve(__dirname, "../examples")
|
|
|
|
|
|
|
|
# Activate all of the languages
|
|
|
|
allLanguages = [
|
|
|
|
"c", "coffee-script", "css", "html",
|
|
|
|
"java", "javascript", "json", "less",
|
|
|
|
"mustache", "objective-c", "perl", "php",
|
|
|
|
"python", "ruby", "sass", "sql",
|
|
|
|
"typescript", "xml", "csharp", "gfm"
|
|
|
|
]
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
for lang in allLanguages
|
|
|
|
do (lang) ->
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.packages.activatePackage("language-#{lang}")
|
|
|
|
|
|
|
|
###
|
|
|
|
Directory structure:
|
2014-12-28 00:23:05 -07:00
|
|
|
- examples
|
|
|
|
- config1
|
|
|
|
- lang1
|
|
|
|
- original
|
|
|
|
- 1 - test.ext
|
|
|
|
- expected
|
|
|
|
- 1 - test.ext
|
|
|
|
- lang2
|
|
|
|
- config2
|
2014-12-27 23:29:03 -07:00
|
|
|
###
|
|
|
|
|
|
|
|
# All Configurations
|
|
|
|
configs = fs.readdirSync(optionsDir)
|
|
|
|
for config in configs
|
|
|
|
do (config) ->
|
|
|
|
# Generate the path to where all of the languages are
|
|
|
|
langsDir = path.resolve(optionsDir, config)
|
|
|
|
optionStats = fs.lstatSync(langsDir)
|
|
|
|
# Confirm that this path is a directory
|
|
|
|
if optionStats.isDirectory()
|
|
|
|
# Create testing group for configuration
|
|
|
|
describe "when using configuration '#{config}'", ->
|
|
|
|
# All Languages for configuration
|
|
|
|
langNames = fs.readdirSync(langsDir)
|
|
|
|
for lang in langNames
|
|
|
|
do (lang) ->
|
|
|
|
# Generate the path to where al of the tests are
|
|
|
|
testsDir = path.resolve(langsDir, lang)
|
|
|
|
langStats = fs.lstatSync(testsDir)
|
|
|
|
# Confirm that this path is a directory
|
|
|
|
if langStats.isDirectory()
|
|
|
|
# Original
|
|
|
|
originalDir = path.resolve(testsDir, "original")
|
|
|
|
if not fs.existsSync(originalDir)
|
|
|
|
console.warn("Directory for test originals/inputs not found." +
|
|
|
|
" Making it at #{originalDir}.")
|
|
|
|
fs.mkdirSync(originalDir)
|
|
|
|
# Expected
|
|
|
|
expectedDir = path.resolve(testsDir, "expected")
|
|
|
|
if not fs.existsSync(expectedDir)
|
|
|
|
console.warn("Directory for test expected/results not found." +
|
|
|
|
"Making it at #{expectedDir}.")
|
|
|
|
fs.mkdirSync(expectedDir)
|
|
|
|
|
|
|
|
# Language group tests
|
|
|
|
describe "when beautifying language '#{lang}'", ->
|
|
|
|
|
|
|
|
# All tests for language
|
|
|
|
testNames = fs.readdirSync(originalDir)
|
|
|
|
for testFileName in testNames
|
|
|
|
do (testFileName) ->
|
|
|
|
ext = path.extname(testFileName)
|
|
|
|
testName = path.basename(testFileName, ext)
|
2014-12-28 00:23:05 -07:00
|
|
|
# If prefixed with underscore (_) then this is a hidden test
|
|
|
|
if testFileName[0] is '_'
|
|
|
|
# Do not show this test
|
|
|
|
return
|
2014-12-27 23:29:03 -07:00
|
|
|
# Confirm this is a test
|
2014-12-28 00:23:05 -07:00
|
|
|
it "#{testName} #{testFileName}", ->
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
# Generate paths to test files
|
|
|
|
originalTestPath = path.resolve(originalDir, testFileName)
|
|
|
|
expectedTestPath = path.resolve(expectedDir, testFileName)
|
|
|
|
# Get contents of original test file
|
|
|
|
originalContents = fs.readFileSync(originalTestPath)?.toString()
|
|
|
|
# Check if there is a matching expected test resut
|
|
|
|
if not fs.existsSync(expectedTestPath)
|
|
|
|
throw new Error("No matching expected test result found for '#{testName}' " +
|
2014-12-28 00:23:05 -07:00
|
|
|
"at '#{expectedTestPath}'.")
|
2014-12-27 23:29:03 -07:00
|
|
|
# err = fs.writeFileSync(expectedTestPath, originalContents)
|
|
|
|
# throw err if err
|
|
|
|
# Get contents of expected test file
|
|
|
|
expectedContents = fs.readFileSync(expectedTestPath)?.toString()
|
|
|
|
# expect(expectedContents).not.toEqual originalContents
|
|
|
|
# expect(atom.grammars.getGrammars()).toEqual []
|
|
|
|
grammar = atom.grammars.selectGrammar(originalTestPath, originalContents)
|
|
|
|
# expect(grammar).toEqual("test")
|
|
|
|
grammarName = grammar.name
|
|
|
|
|
2014-12-28 13:18:30 -07:00
|
|
|
# Get the options
|
2014-12-28 00:23:05 -07:00
|
|
|
allOptions = options.getOptionsForPath(originalTestPath)
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
beautifyCompleted = false
|
|
|
|
completionFun = (text) ->
|
|
|
|
if text instanceof Error
|
|
|
|
return beautifyCompleted = text # text == Error
|
|
|
|
expect(typeof text).toEqual "string"
|
2014-12-28 00:23:05 -07:00
|
|
|
if text isnt expectedContents
|
|
|
|
console.warn(allOptions, text, expectedContents)
|
2014-12-27 23:29:03 -07:00
|
|
|
expect(text).toEqual expectedContents
|
|
|
|
beautifyCompleted = true
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
try
|
|
|
|
beautifier.beautify originalContents, grammarName, allOptions, completionFun
|
|
|
|
catch e
|
|
|
|
beautifyCompleted = e
|
|
|
|
|
|
|
|
waitsFor(->
|
|
|
|
if beautifyCompleted instanceof Error
|
|
|
|
throw beautifyCompleted
|
|
|
|
else
|
|
|
|
return beautifyCompleted
|
|
|
|
, "Waiting for beautification to complete", 5000)
|