1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Test out import-graph output for relevant languages

This commit is contained in:
Timothy Clem 2018-02-07 12:20:41 -08:00
parent fec643536b
commit fb000828ed
11 changed files with 1029 additions and 0 deletions

View File

@ -198,6 +198,7 @@ test-suite test
, Diffing.Algorithm.SES.Spec
, Diffing.Interpreter.Spec
, Integration.Spec
, Rendering.Imports.Spec
, Rendering.TOC.Spec
, Semantic.Spec
, Semantic.CLI.Spec

View File

@ -0,0 +1,52 @@
module Rendering.Imports.Spec where
import Analysis.ConstructorName (ConstructorName, constructorLabel)
import Analysis.Declaration (HasDeclaration, declarationAlgebra)
import Analysis.IdentifierName (IdentifierName, identifierLabel)
import Analysis.ModuleDef (HasModuleDef, moduleDefAlgebra)
import Data.Output
import Parsing.Parser
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.Map as Map
import qualified Semantic.Util as Util
import Rendering.Imports
import Rendering.Renderer
import Rendering.TOC.Spec
import Semantic
import Semantic.Task
import SpecHelpers
import Test.Hspec (Spec, describe, it, parallel, pendingWith)
import Test.Hspec.Expectations.Pretty
import Test.Hspec.LeanCheck
import Test.LeanCheck
spec :: Spec
spec = parallel $ do
describe "renderToImports" $ do
it "works for Ruby" $ do
output <- parseToImports rubyParser "test/fixtures/ruby/import-graph/app.rb"
expected <- readFileVerbatim "test/fixtures/ruby/import-graph/app.json"
toVerbatimOutput output `shouldBe` expected
it "works for Python" $ do
output <- parseToImports pythonParser "test/fixtures/python/import-graph/main.py"
expected <- readFileVerbatim "test/fixtures/python/import-graph/main.json"
toVerbatimOutput output `shouldBe` expected
it "works for Go" $ do
output <- parseToImports goParser "test/fixtures/go/import-graph/main.go"
expected <- readFileVerbatim "test/fixtures/go/import-graph/main.json"
toVerbatimOutput output `shouldBe` expected
it "works for TypeScript" $ do
output <- parseToImports typescriptParser "test/fixtures/typescript/import-graph/app.ts"
expected <- readFileVerbatim "test/fixtures/typescript/import-graph/app.json"
toVerbatimOutput output `shouldBe` expected
where
toVerbatimOutput = verbatim . toOutput
parseToImports parser path = do
blob <- Util.file path
runTask (parse parser blob >>= decorate (declarationAlgebra blob) >>= decorate (moduleDefAlgebra blob) >>= render (renderToImports blob))

View File

@ -11,6 +11,7 @@ import qualified Diffing.Algorithm.SES.Spec
import qualified Diffing.Interpreter.Spec
import qualified Integration.Spec
import qualified Rendering.TOC.Spec
import qualified Rendering.Imports.Spec
import qualified Semantic.Spec
import qualified Semantic.CLI.Spec
import qualified Semantic.IO.Spec
@ -31,6 +32,7 @@ main = hspec $ do
describe "Diffing.Algorithm.SES" Diffing.Algorithm.SES.Spec.spec
describe "Diffing.Interpreter" Diffing.Interpreter.Spec.spec
describe "Rendering.TOC" Rendering.TOC.Spec.spec
describe "Rendering.Imports" Rendering.Imports.Spec.spec
describe "Semantic" Semantic.Spec.spec
describe "Semantic.CLI" Semantic.CLI.Spec.spec
describe "Semantic.IO" Semantic.IO.Spec.spec

13
test/fixtures/go/import-graph/main.go vendored Normal file
View File

@ -0,0 +1,13 @@
package app
import "os"
import (
"net/http"
)
func foo() {}
func main() {
foo()
}

89
test/fixtures/go/import-graph/main.json vendored Normal file
View File

@ -0,0 +1,89 @@
{
"modules": {
"app": {
"imports": [
{
"span": {
"start": [
3,
8
],
"end": [
3,
12
]
},
"path": "os",
"symbols": [],
"alias": "os"
},
{
"span": {
"start": [
6,
2
],
"end": [
6,
12
]
},
"path": "net/http",
"symbols": [],
"alias": "http"
}
],
"name": "app",
"language": "Go",
"declarations": [
{
"span": {
"start": [
9,
1
],
"end": [
9,
14
]
},
"kind": "Function",
"name": "foo"
},
{
"span": {
"start": [
11,
1
],
"end": [
13,
2
]
},
"kind": "Function",
"name": "main"
}
],
"paths": [
"test/fixtures/go/import-graph/main.go"
],
"calls": [
{
"span": {
"start": [
12,
3
],
"end": [
12,
8
]
},
"symbol": "foo",
"targets": []
}
]
}
}
}

View File

@ -0,0 +1,440 @@
{
"modules": {
"main": {
"imports": [
{
"span": {
"start": [
1,
8
],
"end": [
1,
10
]
},
"path": "os",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
2,
8
],
"end": [
2,
19
]
},
"path": "numpy",
"symbols": [],
"alias": "np"
},
{
"span": {
"start": [
3,
8
],
"end": [
3,
9
]
},
"path": "a",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
3,
11
],
"end": [
3,
17
]
},
"path": "b",
"symbols": [],
"alias": "c"
},
{
"span": {
"start": [
4,
8
],
"end": [
4,
16
]
},
"path": "b.c",
"symbols": [],
"alias": "d"
},
{
"span": {
"start": [
4,
18
],
"end": [
4,
19
]
},
"path": "e",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
6,
1
],
"end": [
6,
16
]
},
"path": "b",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
7,
1
],
"end": [
7,
21
]
},
"path": "b",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
8,
1
],
"end": [
8,
16
]
},
"path": "b",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
9,
1
],
"end": [
9,
29
]
},
"path": "b",
"symbols": [
{
"alias": "x",
"name": "a"
},
{
"alias": "y",
"name": "b"
}
],
"alias": ""
}
],
"name": "main",
"language": "Python",
"declarations": [
{
"span": {
"start": [
11,
1
],
"end": [
13,
1
]
},
"kind": "Function",
"name": "print_cwd"
},
{
"span": {
"start": [
14,
1
],
"end": [
17,
1
]
},
"kind": "Function",
"name": "create_array"
},
{
"span": {
"start": [
18,
1
],
"end": [
21,
1
]
},
"kind": "Function",
"name": "sum_array"
},
{
"span": {
"start": [
22,
1
],
"end": [
24,
1
]
},
"kind": "Function",
"name": "sum_array2"
}
],
"paths": [
"test/fixtures/python/import-graph/main.py"
],
"calls": [
{
"span": {
"start": [
12,
5
],
"end": [
12,
23
]
},
"symbol": "print",
"targets": []
},
{
"span": {
"start": [
12,
11
],
"end": [
12,
22
]
},
"symbol": "getcwd",
"targets": [
"os"
]
},
{
"span": {
"start": [
15,
9
],
"end": [
15,
31
]
},
"symbol": "array",
"targets": [
"np"
]
},
{
"span": {
"start": [
19,
13
],
"end": [
19,
20
]
},
"symbol": "sum",
"targets": [
"x"
]
},
{
"span": {
"start": [
23,
12
],
"end": [
23,
19
]
},
"symbol": "sum",
"targets": [
"x"
]
},
{
"span": {
"start": [
26,
5
],
"end": [
26,
45
]
},
"symbol": "print",
"targets": []
},
{
"span": {
"start": [
27,
5
],
"end": [
27,
16
]
},
"symbol": "print_cwd",
"targets": []
},
{
"span": {
"start": [
28,
9
],
"end": [
28,
23
]
},
"symbol": "create_array",
"targets": []
},
{
"span": {
"start": [
29,
13
],
"end": [
29,
25
]
},
"symbol": "sum_array",
"targets": []
},
{
"span": {
"start": [
30,
14
],
"end": [
30,
27
]
},
"symbol": "sum_array2",
"targets": []
},
{
"span": {
"start": [
31,
5
],
"end": [
31,
55
]
},
"symbol": "print",
"targets": []
},
{
"span": {
"start": [
31,
11
],
"end": [
31,
54
]
},
"symbol": "format",
"targets": [
"'-' * 60 + '\\nYour sum is {}'"
]
},
{
"span": {
"start": [
32,
5
],
"end": [
32,
70
]
},
"symbol": "print",
"targets": []
},
{
"span": {
"start": [
32,
11
],
"end": [
32,
58
]
},
"symbol": "format",
"targets": [
"'-' * 60 + '\\nYour sum2 is {}\\n'"
]
}
]
}
}
}

View File

@ -0,0 +1,32 @@
import os
import numpy as np
import a, b as c
import b.c as d, e
from b import a
from b import (c, a)
from b import *
from b import a as x, b as y
def print_cwd():
print(os.getcwd())
def create_array():
x = np.array([1, 2, 3, 4])
return x
def sum_array(x):
x_sum = x.sum()
return x_sum
def sum_array2(x):
return x.sum()
if __name__ == '__main__':
print('-' * 60 + '\nCurrent directory:')
print_cwd()
x = create_array()
x_sum = sum_array(x)
x_sum2 = sum_array2(x)
print('-' * 60 + '\nYour sum is {}'.format(x_sum))
print('-' * 60 + '\nYour sum2 is {}\n'.format(x_sum2) + '-' * 60)

View File

@ -0,0 +1,89 @@
{
"modules": {
"app": {
"imports": [
{
"span": {
"start": [
1,
1
],
"end": [
1,
15
]
},
"path": "json",
"symbols": [],
"alias": ""
},
{
"span": {
"start": [
2,
1
],
"end": [
2,
21
]
},
"path": "a",
"symbols": [],
"alias": ""
}
],
"name": "app",
"language": "Ruby",
"declarations": [
{
"span": {
"start": [
4,
1
],
"end": [
6,
4
]
},
"kind": "Method",
"name": "foo"
}
],
"paths": [
"test/fixtures/ruby/import-graph/app.rb"
],
"calls": [
{
"span": {
"start": [
5,
3
],
"end": [
5,
9
]
},
"symbol": "puts",
"targets": []
},
{
"span": {
"start": [
8,
1
],
"end": [
8,
7
]
},
"symbol": "foo",
"targets": []
}
]
}
}
}

View File

@ -0,0 +1,8 @@
require "json"
require_relative "a"
def foo(x)
puts x
end
foo(1)

View File

@ -0,0 +1,290 @@
{
"modules": {
"app": {
"imports": [
{
"span": {
"start": [
1,
1
],
"end": [
1,
33
]
},
"path": "foo",
"symbols": [],
"alias": "foo"
},
{
"span": {
"start": [
2,
1
],
"end": [
2,
34
]
},
"path": "aardvark",
"symbols": [],
"alias": "name"
},
{
"span": {
"start": [
3,
1
],
"end": [
3,
30
]
},
"path": "ant",
"symbols": [
{
"alias": "",
"name": "member"
}
],
"alias": "ant"
},
{
"span": {
"start": [
4,
1
],
"end": [
4,
46
]
},
"path": "antelope",
"symbols": [
{
"alias": "",
"name": "member1"
}
],
"alias": "antelope"
},
{
"span": {
"start": [
4,
1
],
"end": [
4,
46
]
},
"path": "antelope",
"symbols": [
{
"alias": "",
"name": "member2"
}
],
"alias": "antelope"
},
{
"span": {
"start": [
5,
1
],
"end": [
5,
57
]
},
"path": "ant-eater",
"symbols": [
{
"alias": "",
"name": "member1"
}
],
"alias": "ant-eater"
},
{
"span": {
"start": [
5,
1
],
"end": [
5,
57
]
},
"path": "ant-eater",
"symbols": [
{
"alias": "alias2",
"name": "member2"
}
],
"alias": "ant-eater"
},
{
"span": {
"start": [
6,
1
],
"end": [
6,
70
]
},
"path": "anaconda",
"symbols": [],
"alias": "anaconda"
},
{
"span": {
"start": [
6,
1
],
"end": [
6,
70
]
},
"path": "anaconda",
"symbols": [
{
"alias": "",
"name": "member1"
}
],
"alias": "anaconda"
},
{
"span": {
"start": [
6,
1
],
"end": [
6,
70
]
},
"path": "anaconda",
"symbols": [
{
"alias": "alias2",
"name": "member2"
}
],
"alias": "anaconda"
},
{
"span": {
"start": [
7,
1
],
"end": [
7,
50
]
},
"path": "alligator",
"symbols": [],
"alias": "alligator"
},
{
"span": {
"start": [
7,
1
],
"end": [
7,
50
]
},
"path": "alligator",
"symbols": [],
"alias": "name"
},
{
"span": {
"start": [
8,
1
],
"end": [
8,
22
]
},
"path": "arctic-tern",
"symbols": [],
"alias": "arctic-tern"
},
{
"span": {
"start": [
9,
1
],
"end": [
9,
32
]
},
"path": "../zip",
"symbols": [],
"alias": "zip"
}
],
"name": "app",
"language": "TypeScript",
"declarations": [
{
"span": {
"start": [
11,
1
],
"end": [
11,
52
]
},
"kind": "Function",
"name": "someFunction"
}
],
"paths": [
"test/fixtures/typescript/import-graph/app.ts"
],
"calls": [
{
"span": {
"start": [
13,
1
],
"end": [
13,
27
]
},
"symbol": "someFunction",
"targets": []
}
]
}
}
}

View File

@ -0,0 +1,13 @@
import defaultMember from "foo";
import * as name from "aardvark";
import { member } from "ant";
import { member1 , member2 } from "antelope";
import { member1 , member2 as alias2 } from "ant-eater";
import defaultMember, { member1, member2 as alias2 } from "anaconda";
import defaultMember, * as name from "alligator";
import "arctic-tern";
import zip = require("../zip");
function someFunction(arg1, arg2): string { arg2; };
someFunction(arg1, "arg2");