pasta_lua 0.2.2

Pasta Lua - Lua integration for Pasta DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
-- pasta.act module tests
-- Tests for pasta.act module - token buffer refactoring
-- 親クラスのUI操作トークン蓄積、スポット切り替え検出、build/yield動作を検証
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect

-- Mock actors for testing
local function create_mock_actors()
    return {
        sakura = { name = "さくら", spot = 0 },
        kero = { name = "うにゅう", spot = 1 },
        char2 = { name = "キャラ2", spot = 2 },
    }
end

-- ============================================================================
-- Requirement 1: UI操作トークン蓄積
-- ============================================================================

describe("ACT - surface()", function()
    test("蓄積トークンに { type = 'surface', id = id } を追加する", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:surface(5)

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("surface")
        expect(act.token[1].id):toBe(5)
    end)

    test("文字列IDをサポートする", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:surface("smile")

        expect(act.token[1].id):toBe("smile")
    end)

    test("メソッドチェーン用にselfを返す", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        local returned = act:surface(5)

        expect(returned):toBe(act)
    end)
end)

describe("ACT - wait()", function()
    test("蓄積トークンに { type = 'wait', ms = ms } を追加する", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:wait(500)

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("wait")
        expect(act.token[1].ms):toBe(500)
    end)

    test("負の値は0に変換する", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:wait(-100)

        expect(act.token[1].ms):toBe(0)
    end)

    test("小数点以下を切り捨てる", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:wait(500.7)

        expect(act.token[1].ms):toBe(500)
    end)

    test("nilは0として扱う", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:wait(nil)

        expect(act.token[1].ms):toBe(0)
    end)

    test("メソッドチェーン用にselfを返す", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        local returned = act:wait(500)

        expect(returned):toBe(act)
    end)
end)

describe("ACT - newline()", function()
    test("蓄積トークンに { type = 'newline', n = n } を追加する", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:newline(3)

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("newline")
        expect(act.token[1].n):toBe(3)
    end)

    test("引数なしの場合はn=1をデフォルトとする", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:newline()

        expect(act.token[1].n):toBe(1)
    end)

    test("メソッドチェーン用にselfを返す", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        local returned = act:newline()

        expect(returned):toBe(act)
    end)
end)

describe("ACT - clear()", function()
    test("蓄積トークンに { type = 'clear' } を追加する", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        act:clear()

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("clear")
    end)

    test("メソッドチェーン用にselfを返す", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        local returned = act:clear()

        expect(returned):toBe(act)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: set_spot()トークン化 (Task 1.1)
-- ============================================================================

describe("ACT - set_spot() トークン化", function()
    test("set_spot()が正しいトークン構造{type='spot', actor=Actor, spot=N}を生成する", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:set_spot("sakura", 0)

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("spot")
        expect(act.token[1].actor):toBe(actors.sakura)
        expect(act.token[1].spot):toBe(0)
    end)

    test("set_spot()呼び出し後もactor.spotが変更されていない(状態レス化)", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local original_spot = actors.sakura.spot
        local act = ACT.new(actors)

        act:set_spot("sakura", 5)

        -- actorオブジェクトの状態は変更されていない
        expect(actors.sakura.spot):toBe(original_spot)
    end)

    test("set_spot()でspotトークンを生成後、actorのspot属性は不変", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        -- sakuraのspotは0
        local before = actors.sakura.spot
        act:set_spot("sakura", 99)
        local after = actors.sakura.spot

        expect(before):toBe(after)
        expect(act.token[1].spot):toBe(99)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: clear_spot()トークン化 (Task 1.2)
-- ============================================================================

describe("ACT - clear_spot() トークン化", function()
    test("clear_spot()が正しいトークン構造{type='clear_spot'}を生成する", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:clear_spot()

        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("clear_spot")
    end)

    test("clear_spot()呼び出し後もactorオブジェクトが変更されていない", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        -- 最初にスポットを設定しておく
        actors.sakura.spot = 0
        actors.kero.spot = 1

        act:clear_spot()

        -- actorオブジェクトの状態は変更されていない
        expect(actors.sakura.spot):toBe(0)
        expect(actors.kero.spot):toBe(1)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: talk()のactor情報追加 (Task 1.3)
-- ============================================================================

describe("ACT - talk() actor情報付きトークン生成", function()
    test("talk()が{type='talk', actor=Actor, text=text}トークンを生成する", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "こんにちは")

        -- talk トークンにactor情報が含まれる
        local talk_token = nil
        for _, t in ipairs(act.token) do
            if t.type == "talk" then
                talk_token = t
                break
            end
        end

        expect(talk_token):toBeTruthy()
        expect(talk_token.actor):toBe(actors.sakura)
        expect(talk_token.text):toBe("こんにちは")
    end)

    test("talk()がactorトークンを生成しない(状態レス化)", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "Hello")
        act:talk(actors.kero, "Hi")

        -- {type="actor"}トークンが存在しない
        local has_actor_token = false
        for _, t in ipairs(act.token) do
            if t.type == "actor" then
                has_actor_token = true
            end
        end

        expect(has_actor_token):toBe(false)
    end)

    test("talk()がspot_switchトークンを生成しない(状態レス化)", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "Hello")
        act:talk(actors.kero, "Hi")

        -- {type="spot_switch"}トークンが存在しない
        local has_spot_switch = false
        for _, t in ipairs(act.token) do
            if t.type == "spot_switch" then
                has_spot_switch = true
            end
        end

        expect(has_spot_switch):toBe(false)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: 状態フィールド削除 (Task 1.4)
-- ============================================================================

describe("ACT - 状態フィールド削除", function()
    test("ACT.new()でnow_actorフィールドが存在しない", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        expect(act.now_actor):toBe(nil)
    end)

    test("ACT.new()で_current_spotフィールドが存在しない", function()
        local ACT = require("pasta.act")
        local act = ACT.new(create_mock_actors())

        expect(act._current_spot):toBe(nil)
    end)

    test("talk()呼び出し後もnow_actorが設定されない", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "Hello")

        expect(act.now_actor):toBe(nil)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: アクタープロキシの独立性 (Task 4.2)
-- ============================================================================

describe("ACT - アクタープロキシの独立性", function()
    test("set_spot()呼び出しなしでact.さくら:talk()が動作する", function()
        local ACT = require("pasta.act")
        local actors = { ["さくら"] = { name = "さくら", spot = 0 } }
        local act = ACT.new(actors)

        -- set_spot()なしでプロキシ経由のtalk()を呼び出し
        act["さくら"]:talk("こんにちは")

        local talk_token = nil
        for _, t in ipairs(act.token) do
            if t.type == "talk" then
                talk_token = t
                break
            end
        end

        expect(talk_token):toBeTruthy()
        expect(talk_token.text):toBe("こんにちは")
    end)

    test("プロキシ経由のtalk()が正しいトークン(actor付き)を生成する", function()
        local ACT = require("pasta.act")
        local actors = { ["さくら"] = { name = "さくら", spot = 0 } }
        local act = ACT.new(actors)

        act["さくら"]:talk("テスト")

        local talk_token = nil
        for _, t in ipairs(act.token) do
            if t.type == "talk" then
                talk_token = t
                break
            end
        end

        expect(talk_token):toBeTruthy()
        expect(talk_token.actor):toBe(actors["さくら"])
        expect(talk_token.text):toBe("テスト")
    end)
end)

-- ============================================================================
-- Requirement 5: talk後の固定改行除去
-- ============================================================================

describe("ACT - talk()固定改行除去", function()
    test("talk()後にnewlineトークンを自動挿入しない", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "Hello")

        -- newlineトークンが含まれていないことを確認
        local has_newline = false
        for _, t in ipairs(act.token) do
            if t.type == "newline" then
                has_newline = true
            end
        end
        expect(has_newline):toBe(false)
    end)
end)

-- ============================================================================
-- Requirement 7: 親build()メソッド(グループ化対応版)
-- ============================================================================

describe("ACT - build()", function()
    test("トークン配列を返却する", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        -- グループ化後: talkがないとactorグループが作成されないため、
        -- talkを追加してsurface/waitをグループ内に含める
        act:talk(actors.sakura, "test")
        act:surface(5)
        act:wait(100)
        local result = act:build()

        expect(type(result)):toBe("table")
        expect(#result):toBe(1)
        expect(result[1].type):toBe("actor")
        -- グループ内に3トークン: talk(統合済み), surface, wait
        -- 注: 連続talkは統合されるが、ここでは1つのみ
        expect(#result[1].tokens):toBe(3)
        expect(result[1].tokens[1].type):toBe("talk")
        expect(result[1].tokens[2].type):toBe("surface")
        expect(result[1].tokens[3].type):toBe("wait")
    end)

    test("build()後にself.tokenを空配列にリセットする", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:surface(5)
        local _ = act:build()

        expect(#act.token):toBe(0)
    end)

    test("トークン0件時はnilを返す (act-build-early-return)", function()
        local ACT = require("pasta.act")
        local act = ACT.new({})

        local token = act:build()

        expect(token):toBe(nil)
    end)
end)

-- ============================================================================
-- Requirement 8: 親yield()責務統一(グループ化対応版)
-- ============================================================================

describe("ACT - yield()", function()
    test("yield()はself:build()を呼び出す", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        -- グループ化後: talkでactorグループを作成し、surfaceを含める
        act:talk(actors.sakura, "test")
        act:surface(5)

        local co = coroutine.create(function()
            return act:yield()
        end)

        local ok, result = coroutine.resume(co)

        expect(ok):toBe(true)
        expect(type(result)):toBe("table") -- build()の結果
        expect(#result):toBe(1)
        expect(result[1].type):toBe("actor")
        -- グループ内: talk(統合済み), surface
        expect(#result[1].tokens):toBe(2)
        expect(result[1].tokens[1].type):toBe("talk")
        expect(result[1].tokens[2].type):toBe("surface")
    end)

    test("yield()後にトークンがリセットされる", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "test")
        act:surface(5)

        local co = coroutine.create(function()
            return act:yield()
        end)

        local _, _ = coroutine.resume(co)

        expect(#act.token):toBe(0)
    end)

    test("yield()はメソッドチェーン用にselfを返す", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        act:talk(actors.sakura, "test")
        act:surface(5)

        local co = coroutine.create(function()
            local returned = act:yield()
            return returned
        end)

        -- 最初のresume: yield()でサスペンド
        local ok1, _ = coroutine.resume(co)
        expect(ok1):toBe(true)

        -- 2回目のresume: selfを返す
        local ok2, returned = coroutine.resume(co)
        expect(ok2):toBe(true)
        expect(returned):toBe(act)
    end)
end)

-- ============================================================================
-- Requirement 11: end_action()の削除
-- ============================================================================

describe("ACT - end_action()削除", function()
    test("end_action()メソッドが存在しない", function()
        local ACT = require("pasta.act")
        local act = ACT.new({})

        expect(act.end_action):toBe(nil)
    end)
end)

-- ============================================================================
-- メソッドチェーンテスト
-- ============================================================================

describe("ACT - メソッドチェーン", function()
    test("複数メソッドを連続呼び出し可能", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        local returned = act
            :surface(5)
            :wait(100)
            :newline(2)
            :clear()

        expect(returned):toBe(act)
        expect(#act.token):toBe(4)
    end)

    test("talk()もメソッドチェーンに対応", function()
        local ACT = require("pasta.act")
        local actors = create_mock_actors()
        local act = ACT.new(actors)

        local returned = act:talk(actors.sakura, "Hello")

        expect(returned):toBe(act)
    end)
end)