Skip to content

Commit

Permalink
update: 単体テストを追加した
Browse files Browse the repository at this point in the history
  • Loading branch information
yutotnh committed May 21, 2023
1 parent 5cd20a4 commit e38c19e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function activate(context: vscode.ExtensionContext) {
// ファイルを保存した時に、EUC-JPのファイルの全角チルダを波ダッシュに変換する
vscode.workspace.onDidSaveTextDocument((document) => {
const config = vscode.workspace.getConfiguration("waveDashUnify");
if (!config.get("enable")) {return;}
if (!config.get("enable")) { return; }

const filePath = document.uri.fsPath;

Expand Down Expand Up @@ -36,7 +36,7 @@ export function activate(context: vscode.ExtensionContext) {
* @param str 判定する文字列
* @returns `true`: EUC-JP, `false`: EUC-JP以外
*/
function isEUCJP(str: Buffer): boolean {
export function isEUCJP(str: Buffer): boolean {
const Encoding = require("encoding-japanese");

return Encoding.detect(str, "EUCJP") === "EUCJP";
Expand All @@ -48,7 +48,7 @@ function isEUCJP(str: Buffer): boolean {
* @param str 変換元の文字列
* @returns 変換後の文字列
*/
function replaceFullWidthTildeToWaveDash(str: Buffer): Buffer {
export function replaceFullWidthTildeToWaveDash(str: Buffer): Buffer {
let convertedString: number[] = new Array(str.length);
let convertedStringIndex = 0;
for (let i = 0; i < str.length; i++) {
Expand Down
65 changes: 62 additions & 3 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as assert from 'assert';
import * as extension from '../../extension';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
Expand All @@ -8,8 +9,66 @@ import * as vscode from 'vscode';
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');

test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
/**
* 文字列がEUC-JPかを判定する関数をテストする
*/
test('detect EUC-JP', () => {
const eucjpContents = [
// 全角チルダのみ
Buffer.from([0x8F, 0xA2, 0xB7]),
Buffer.from([0x8F, 0xA2, 0xB7, 0x8F, 0xA2, 0xB7]),

// 全角チルダの前にASCII文字
Buffer.from([0x31, 0x8F, 0xA2, 0xB7, 0x8F, 0xA2, 0xB7, 0x32]),
];

eucjpContents.forEach(content => {
assert.strictEqual(true, extension.isEUCJP(content), `content: ${content.toString('hex')}`);
});

const notEucjpContents = [
// ASCIIのみのテキストはEUC-JPと判断されるため、テストしない
// 文字列: 123
// Buffer.from([0x31, 0x32, 0x33]),

// Shift-JISのテキスト
// 文字列: こんにちは
Buffer.from([0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD]),

// UTF-8のテキスト
// 文字列: よろしく
Buffer.from([0xE3, 0x82, 0x88, 0xE3, 0x82, 0x8D, 0xE3, 0x81, 0x97, 0xE3, 0x81, 0x8F]),
];

notEucjpContents.forEach(content => {
assert.strictEqual(false, extension.isEUCJP(content), `content: ${content.toString('hex')}`);
});
});

/**
* 全角チルダを波ダッシュに変換する関数をテストする
*/
test('replace full-width tilde to wave dash', () => {
const contents = [
// 全角チルダのみ
{
before: Buffer.from([0x8F, 0xA2, 0xB7]),
after: Buffer.from([0xA1, 0xC1]),
},
{
before: Buffer.from([0x8F, 0xA2, 0xB7, 0x8F, 0xA2, 0xB7]),
after: Buffer.from([0xA1, 0xC1, 0xA1, 0xC1]),
},

// 全角チルダの前後にASCII文字
{
before: Buffer.from([0x31, 0x8F, 0xA2, 0xB7, 0x8F, 0xA2, 0xB7, 0x32]),
after: Buffer.from([0x31, 0xA1, 0xC1, 0xA1, 0xC1, 0x32]),
},
];

contents.forEach(content => {
assert.strictEqual(content.after.toString('hex'), extension.replaceFullWidthTildeToWaveDash(content.before).toString('hex'), `content: ${content.before.toString('hex')}`);
});
});
});

0 comments on commit e38c19e

Please sign in to comment.