Skip to content

Commit

Permalink
Merge pull request #7 from teknologi-umum/test-1
Browse files Browse the repository at this point in the history
refactor test
  • Loading branch information
Reinaldy Rafli authored Aug 14, 2021
2 parents 125e7d8 + be5d3ac commit 238e4bc
Show file tree
Hide file tree
Showing 13 changed files with 658 additions and 98 deletions.
28 changes: 27 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"lint": "eslint --fix --ignore-path .gitignore .",
"format": "prettier --write --ignore-path .gitignore .",
"prepare": "node prepare.cjs",
"test": "c8 uvu -r esbuild-register tests \".(test|spec).ts\"",
"test:unit": "uvu -r esbuild-register tests \".(test|spec).ts\"",
"test:coverage": "c8 npm run test:unit",
"test:tdd": "npm run test:unit; watchlist src/ -- npm run test:unit",
"test": "npm run test:coverage",
"build": "rollup -c"
},
"files": [
Expand Down Expand Up @@ -59,6 +62,7 @@
"prettier": "^2.3.2",
"rollup": "^2.56.1",
"typescript": "^4.3.5",
"uvu": "^0.5.1"
"uvu": "^0.5.1",
"watchlist": "^0.2.3"
}
}
96 changes: 84 additions & 12 deletions tests/c.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import * as assert from 'uvu/assert';
import detectLang from '../src/index';

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

test('fizz buzz', () => {
assert.equal(
'C',
detectLang(`#include <stdio.h>
const code = detectLang(`#include <stdio.h>
int main(void)
{
Expand All @@ -30,27 +29,100 @@ test('fizz buzz', () => {
}
return 0;
}`),
);
}`);
assert.equal(code, 'C');
});

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

test('file', () => {
assert.equal(
'C',
detectLang(`static int IndexEntry__set_mtime__meth(lua_State *L) {
const code = 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;
}`),
);
}`);
assert.equal(code, 'C');
});

test('quick sort', () => {
const code = detectLang(`#include <stdio.h>
void quicksort(int *A, int len);
int main (void) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
quicksort(a, n);
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void quicksort(int *A, int len) {
if (len < 2) return;
int pivot = A[len / 2];
int i, j;
for (i = 0, j = len - 1; ; i++, j--) {
while (A[i] < pivot) i++;
while (A[j] > pivot) j--;
if (i >= j) break;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
quicksort(A, i);
quicksort(A + i, len - i);
}`);
assert.equal(code, 'C');
});

test('http server', () => {
const code = detectLang(`#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int
main(void)
{
CURL *curl;
char buffer[CURL_ERROR_SIZE];
if ((curl = curl_easy_init()) != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.rosettacode.org/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer);
if (curl_easy_perform(curl) != CURLE_OK) {
fprintf(stderr, "%s\n", buffer);
return EXIT_FAILURE;
}
curl_easy_cleanup(curl);
}
return EXIT_SUCCESS;
}`);
assert.equal(code, 'C');
});

test.run();
152 changes: 146 additions & 6 deletions tests/cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import * as assert from 'uvu/assert';
import detectLang from '../src/index';

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

test('fizz buzz', () => {
assert.equal(
'C++',
detectLang(`/*
const code = detectLang(`/*
* fizzbuzz.cpp
*
* Created on: Apr 25, 2012
Expand Down Expand Up @@ -49,8 +48,149 @@ test('fizz buzz', () => {
{
FizzBuzz<100> p;
return 0;
}`),
);
}`);
assert.equal(code, 'C++');
});

test('quick sort', () => {
const code = detectLang(`#include <iterator>
#include <algorithm> // for std::partition
#include <functional> // for std::less
// helper function for median of three
template<typename T>
T median(T t1, T t2, T t3)
{
if (t1 < t2)
{
if (t2 < t3)
return t2;
else if (t1 < t3)
return t3;
else
return t1;
}
else
{
if (t1 < t3)
return t1;
else if (t2 < t3)
return t3;
else
return t2;
}
}
// helper object to get <= from <
template<typename Order> struct non_strict_op:
public std::binary_function<typename Order::second_argument_type,
typename Order::first_argument_type,
bool>
{
non_strict_op(Order o): order(o) {}
bool operator()(typename Order::second_argument_type arg1,
typename Order::first_argument_type arg2) const
{
return !order(arg2, arg1);
}
private:
Order order;
};
template<typename Order> non_strict_op<Order> non_strict(Order o)
{
return non_strict_op<Order>(o);
}
template<typename RandomAccessIterator,
typename Order>
void quicksort(RandomAccessIterator first, RandomAccessIterator last, Order order)
{
if (first != last && first+1 != last)
{
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
RandomAccessIterator mid = first + (last - first)/2;
value_type pivot = median(*first, *mid, *(last-1));
RandomAccessIterator split1 = std::partition(first, last, std::bind2nd(order, pivot));
RandomAccessIterator split2 = std::partition(split1, last, std::bind2nd(non_strict(order), pivot));
quicksort(first, split1, order);
quicksort(split2, last, order);
}
}
template<typename RandomAccessIterator>
void quicksort(RandomAccessIterator first, RandomAccessIterator last)
{
quicksort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
A simpler version of the above that just uses the first element as the pivot and only does one "partition".
#include <iterator>
#include <algorithm> // for std::partition
#include <functional> // for std::less
template<typename RandomAccessIterator,
typename Order>
void quicksort(RandomAccessIterator first, RandomAccessIterator last, Order order)
{
if (last - first > 1)
{
RandomAccessIterator split = std::partition(first+1, last, std::bind2nd(order, *first));
std::iter_swap(first, split-1);
quicksort(first, split-1, order);
quicksort(split, last, order);
}
}
template<typename RandomAccessIterator>
void quicksort(RandomAccessIterator first, RandomAccessIterator last)
{
quicksort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}`);
assert.equal(code, 'C++');
});

// FIXME: This detected as C.
test.skip('http server', () => {
const code = detectLang(`#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
int main() {
WSADATA wsaData;
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
addrinfo *result = NULL;
addrinfo hints;
ZeroMemory( &hints, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo( "74.125.45.100", "80", &hints, &result ); // http://www.google.com
SOCKET s = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
connect( s, result->ai_addr, (int)result->ai_addrlen );
freeaddrinfo( result );
send( s, "GET / HTTP/1.0\n\n", 16, 0 );
char buffer[512];
int bytes;
do {
bytes = recv( s, buffer, 512, 0 );
if ( bytes > 0 )
std::cout.write(buffer, bytes);
} while ( bytes > 0 );
return 0;
}`);
assert.equal(code, 'C++');
});

test.run();
11 changes: 5 additions & 6 deletions tests/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ 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}'));
const code = detectLang('.hello-world {\n\tfont-size: 100px;\n}');
assert.equal(code, 'CSS');
});

test('long', () => {
assert.equal(
'CSS',
detectLang(`/**
const code = detectLang(`/**
* Improve readability when focused and also mouse hovered in all browsers.
*/
Expand All @@ -24,8 +23,8 @@ test('long', () => {
abbr[title] {
border-bottom: 1px dotted;
}`),
);
}`);
assert.equal(code, 'CSS');
});

test.run();
Loading

0 comments on commit 238e4bc

Please sign in to comment.