Skip to content

Commit

Permalink
test: should be done per language, not test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinaldy Rafli committed Aug 8, 2021
1 parent 122d78c commit 91e59f4
Show file tree
Hide file tree
Showing 24 changed files with 368 additions and 5,243 deletions.
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ const languages: Record<string, LanguagePattern[]> = {
Unknown: [],
};

function getPoints(lineOfCode: string, checkers: LanguagePattern[]) {
/**
* TODO: FILL THIS
* @param {String} lineOfCode
* @param {LanguagePattern[]} checkers
* @returns {Number}
*/
function getPoints(lineOfCode: string, checkers: LanguagePattern[]): number {
const checker: number[] = checkers.map((o) => {
if (o.pattern.test(lineOfCode)) return o.points;
return 0;
Expand All @@ -66,6 +72,17 @@ export interface StatisticOutput {
statistics: (string | number)[][];
}

/**
* TODO: FILL THIS
* @param {String} snippet The code we're guessing
* @param {Options} options Options
* @returns {String | StatisticOutput}
* @example
* ```js
* import detectLang from 'package-name';
* const detect = detectLang(code);
* ```
*/
function detectLang(
snippet: string,
options: Options = { heuristic: true, statistics: false },
Expand All @@ -75,7 +92,7 @@ function detectLang(
.replace(/\n{2,}/g, '\n')
.split('\n');

function nearTop(index: number) {
function nearTop(index: number): boolean {
if (linesOfCode.length <= 10) {
return true;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/c.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('hello world', () => {
assert.equal('C', detectLang('printf("Hello world!\\n");'));
});

test('fizz buzz', () => {
assert.equal(
'C',
detectLang(`#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 100; ++i)
{
if (i % 3 == 0)
printf("Fizz");
if (i % 5 == 0)
printf("Buzz");
if ((i % 3 != 0) && (i % 5 != 0))
printf("%d", i);
printf("\n");
}
return 0;
}`),
);
});

test('variable declaration', () => {
assert.equal('C', detectLang('int *ptr;'));
});

test('file', () => {
assert.equal(
'C',
detectLang(`static int IndexEntry__set_mtime__meth(lua_State *L) {
IndexEntry * this_idx1 = obj_type_IndexEntry_check(L,1);
git_time_t secs_idx2 = luaL_checkinteger(L,2);
unsigned int nanosecs_idx3 = luaL_checkinteger(L,3);
this_idx1->mtime.seconds = secs_idx2;
this_idx1->mtime.nanoseconds = nanosecs_idx3;
return 0;
}`),
);
});

test.run();
56 changes: 56 additions & 0 deletions tests/cpp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('hello world', () => {
assert.equal('C++', detectLang('cout << "Hello world" << endl;'));
});

test('fizz buzz', () => {
assert.equal(
'C++',
detectLang(`/*
* fizzbuzz.cpp
*
* Created on: Apr 25, 2012
* Author: Brian Geffon
*
* fizzbuzz solved without looping or conditionals using only template recursion.
*/
#include <iostream>
#include <string>
template <int r> struct FizzBuzzPrinter {
static const int fizzBuzzed = 0;
template <typename T> FizzBuzzPrinter(T t) {}
};
template <> struct FizzBuzzPrinter<0> {
static const int fizzBuzzed = 1;
template <typename T> FizzBuzzPrinter(T t) {
std::cout << t;
}
};
template <int N> struct FizzBuzz: FizzBuzz<N - 1> {
FizzBuzz() {
FizzBuzzPrinter<(N % 15)>("FizzBuzz");
FizzBuzzPrinter<(N % 5) + FizzBuzzPrinter<N % 15>::fizzBuzzed>("Buzz");
FizzBuzzPrinter<(N % 3) + FizzBuzzPrinter<(N % 15)>::fizzBuzzed + FizzBuzzPrinter<(N % 5) + FizzBuzzPrinter<N % 15>::fizzBuzzed>::fizzBuzzed>("Fizz");
FizzBuzzPrinter<FizzBuzzPrinter<N % 3>::fizzBuzzed + FizzBuzzPrinter<N % 5>::fizzBuzzed>(int(N));
std::cout << std::endl;
}
};
template <> struct FizzBuzz<0> {};
int main (int argc, char **argv)
{
FizzBuzz<100> p;
return 0;
}`),
);
});

test.run();
31 changes: 31 additions & 0 deletions tests/css.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('hello world', () => {
assert.equal('CSS', detectLang('.hello-world {\n\tfont-size: 100px;\n}'));
});

test('long', () => {
assert.equal(
'CSS',
detectLang(`/**
* Improve readability when focused and also mouse hovered in all browsers.
*/
a:active,
a:hover {
outline: 0;
}
/**
* Address styling not present in IE 8/9/10/11, Safari, and Chrome.
*/
abbr[title] {
border-bottom: 1px dotted;
}`),
);
});

test.run();
22 changes: 0 additions & 22 deletions tests/fizzbuzz/fizzbuzz.c

This file was deleted.

41 changes: 0 additions & 41 deletions tests/fizzbuzz/fizzbuzz.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions tests/fizzbuzz/fizzbuzz.java

This file was deleted.

7 changes: 0 additions & 7 deletions tests/fizzbuzz/fizzbuzz.js

This file was deleted.

17 changes: 0 additions & 17 deletions tests/fizzbuzz/fizzbuzz.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/fizzbuzz/fizzbuzz.py

This file was deleted.

11 changes: 0 additions & 11 deletions tests/fizzbuzz/fizzbuzz.rb

This file was deleted.

19 changes: 17 additions & 2 deletions tests/fizzbuzz/fizzbuzz.go → tests/go.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package main
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('hello world', () => {
assert.equal('Go', detectLang('fmt.Println("Hello world")'));
});

test('fizz buzz', () => {
assert.equal(
'Go',
detectLang(`package main
import (
"fmt"
Expand Down Expand Up @@ -27,4 +38,8 @@ func fizzbuzz(i int) {
} else {
fmt.Println(i)
}
}
}`),
);
});

test.run();
Loading

0 comments on commit 91e59f4

Please sign in to comment.