-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds TestUASTGetLanguagesSuite/TestSameEnryLanguage #412
Open
se7entyse7en
wants to merge
4
commits into
src-d:master
Choose a base branch
from
se7entyse7en:test-enry-bblfsh-lang-labels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,157 @@ package handler_test | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/pressly/lg" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"gopkg.in/src-d/enry.v1" | ||
|
||
"github.com/src-d/gitbase-web/server/handler" | ||
"github.com/src-d/gitbase-web/server/serializer" | ||
"github.com/src-d/gitbase-web/server/service" | ||
) | ||
|
||
type UASTGetLanguagesSuite struct { | ||
suite.Suite | ||
handler http.Handler | ||
} | ||
|
||
func TestUASTGetLanguagesSuite(t *testing.T) { | ||
if !isIntegration() { | ||
t.Skip("use the env var GITBASEPG_INTEGRATION_TESTS=true to run this test") | ||
} | ||
|
||
q := new(UASTGetLanguagesSuite) | ||
r := chi.NewRouter() | ||
r.Use(lg.RequestLogger(logrus.New())) | ||
r.Post("/detect-lang", handler.APIHandlerFunc(handler.DetectLanguage())) | ||
r.Get("/get-languages", handler.APIHandlerFunc(handler.GetLanguages(bblfshServerURL()))) | ||
|
||
q.handler = r | ||
|
||
suite.Run(t, q) | ||
} | ||
|
||
func UnmarshalGetLanguagesResponse(b []byte) []service.Language { | ||
var resBody struct { | ||
Data []service.Language `json:"data"` | ||
} | ||
json.Unmarshal(b, &resBody) | ||
return resBody.Data | ||
} | ||
|
||
func (suite *UASTGetLanguagesSuite) TestSameEnryLanguage() { | ||
req, _ := http.NewRequest("GET", "/get-languages", strings.NewReader("")) | ||
|
||
res := httptest.NewRecorder() | ||
suite.handler.ServeHTTP(res, req) | ||
|
||
suite.Require().Equal(http.StatusOK, res.Code, res.Body.String()) | ||
|
||
escapeForJSON := func(s string) string { | ||
return strings.Replace(strings.Replace(s, "\"", "\\\"", -1), | ||
"\n", "\\n", -1) | ||
} | ||
|
||
for _, lang := range UnmarshalGetLanguagesResponse(res.Body.Bytes()) { | ||
langName := lang.Name | ||
suite.T().Run(langName, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
content, filename := suite.getContentAndFilename(langName) | ||
jsonRequest := fmt.Sprintf(`{ "content": "%s", "filename": "%s" }`, | ||
escapeForJSON(content), filename) | ||
req, _ := http.NewRequest("POST", "/detect-lang", strings.NewReader(jsonRequest)) | ||
|
||
res = httptest.NewRecorder() | ||
suite.handler.ServeHTTP(res, req) | ||
|
||
require.Equal(http.StatusOK, res.Code, res.Body.String()) | ||
se7entyse7en marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
detectedLang, detectedLangType := handler.UnmarshalDetectLangResponse(res.Body.Bytes()) | ||
|
||
if langName == "Bash" { | ||
require.NotEqual(langName, detectedLang) | ||
t.Skip("TEST FAILURE IS A KNOWN ISSUE") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? Isn't this due to the aliasing problem that we also have on engine? But shouldn't it be true also for |
||
} | ||
|
||
require.Equal(langName, detectedLang) | ||
require.Equal(enry.Programming, detectedLangType) | ||
}) | ||
} | ||
} | ||
|
||
func (suite *UASTGetLanguagesSuite) getContentAndFilename(lang string) (string, string) { | ||
suite.T().Helper() | ||
|
||
switch lang { | ||
case "Bash": | ||
return "echo 'Hello World!'", "hello.sh" | ||
case "C#": | ||
return ` | ||
class HelloWorldProgram | ||
{ | ||
public static void Main() | ||
{ | ||
System.Console.WriteLine("Hello, world!"); | ||
} | ||
} | ||
`, "hello.cs" | ||
case "C++": | ||
return ` | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Hello World!" << std::endl; | ||
return 0; | ||
} | ||
`, "hello.cpp" | ||
case "Go": | ||
return ` | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("Hello World!") | ||
} | ||
`, "hello.go" | ||
case "Java": | ||
return ` | ||
public class HelloWorld { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
} | ||
|
||
} | ||
`, "hello.java" | ||
case "JavaScript": | ||
return "console.log('Hello World!')", "hello.js" | ||
case "PHP": | ||
return ` | ||
<?php | ||
echo "Hello World!"; | ||
?> | ||
`, "hello.php" | ||
case "Python": | ||
return "print('Hello World!')", "hello.py" | ||
case "Ruby": | ||
return "puts 'Hello world!'", "hello.rb" | ||
} | ||
|
||
return "", "" | ||
} | ||
|
||
type UASTParseSuite struct { | ||
suite.Suite | ||
handler http.Handler | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this manual escape you could do
or export
detectLangRequest
type andThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the tip! Done here.