訂判斷及所需輸入
題內英語及香港中文設定各自訂字樣、冒號空格及單位標點。中文零數量文件須顯示「合共:0 件。」,lang 亦為 zh-HK。只改語言屬性、文字仍英文,未算套對設定。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"locale":"zh-HK","quantity":0} |
跑所附比較
程序核真正文字、文章語言及未改數量。兩套字樣由本例提供,不聲稱自動翻譯已等價。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"lang":"zh-HK","text":"Total: 0 units.","quantity":"0"} |
| 所需路徑 | {"lang":"zh-HK","text":"合共:0 件。","quantity":"0"} |
import assert from 'node:assert/strict';
const smallWhole=(x,max)=>Number.isSafeInteger(x)&&x>=0&&x<=max;
import {createRequire} from 'node:module';
const htmlEscape=s=>String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
async function withPage(action,options={}){
const require=createRequire(import.meta.url);const {chromium}=require('playwright');
const browser=await chromium.launch({headless:true});
try{const context=await browser.newContext(options);const page=await context.newPage();
const requests=[];await page.route('**/*',route=>{requests.push(route.request().url());return route.abort();});
return await action(page,context,requests);
}finally{await browser.close();}
}
const run=async function run(i,corrected=true){
const profiles={en:{label:'Total',colon:': ',unit:' units.'},'zh-HK':{label:'合共',colon:':',unit:' 件。'}};
if(!Object.hasOwn(profiles,i.locale))return {held:true,reason:'profile-missing'};
if(!smallWhole(i.quantity,1000))return {held:true,reason:'quantity-missing-or-invalid'};
return withPage(async page=>{const p=profiles[corrected?i.locale:'en'];await page.setContent('<article lang="'+i.locale+'"><p>'+p.label+p.colon+i.quantity+p.unit+'</p><data value="'+i.quantity+'"></data></article>');
return page.evaluate(()=>({lang:document.querySelector('article').lang,text:document.querySelector('p').textContent,quantity:document.querySelector('data').value}));});
};
const cases=[{"input":{"locale":"zh-HK","quantity":0},"corrected":false,"expected":{"lang":"zh-HK","text":"Total: 0 units.","quantity":"0"}},{"input":{"locale":"zh-HK","quantity":0},"corrected":true,"expected":{"lang":"zh-HK","text":"合共:0 件。","quantity":"0"}},{"input":{"locale":"en","quantity":3},"corrected":true,"expected":{"lang":"en","text":"Total: 3 units.","quantity":"3"}},{"input":{"locale":"fr","quantity":3},"corrected":true,"expected":{"held":true,"reason":"profile-missing"}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
按邊界依據作判斷
英文三件呈 Total: 3 units.;題內沒有法文設定,故暫停,不靜靜轉英文。
- 選所附語言設定
- 呈現字樣與數量
- 核真正文字及語言
所需語言沒有設定便暫停。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 沿英文設定 | {"locale":"en","quantity":3} | {"lang":"en","text":"Total: 3 units.","quantity":"3"} |
| 未有法文設定 | {"locale":"fr","quantity":3} | {"held":true,"reason":"profile-missing"} |
原例只設 lang=zh-HK,文件就完成了嗎?
查看參考答案
不是,真正字樣及標點也須沿中文設定;兩版數量都維持零。