File: font_metric_cache_spec.rb

package info (click to toggle)
ruby-prawn 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,380 kB
  • sloc: ruby: 15,820; sh: 43; makefile: 20
file content (54 lines) | stat: -rw-r--r-- 1,518 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'spec_helper'
require 'pathname'

describe Prawn::FontMetricCache do
  let(:document) { Prawn::Document.new }
  let(:font_metric_cache) { described_class.new(document) }

  it 'starts with an empty cache' do
    expect(font_metric_cache.instance_variable_get(:@cache)).to be_empty
  end

  it 'caches the width of the provided string' do
    font_metric_cache.width_of('M', {})

    expect(font_metric_cache.instance_variable_get(:@cache).size).to eq(1)
  end

  it 'onlies cache a single copy of the same string' do
    font_metric_cache.width_of('M', {})
    font_metric_cache.width_of('M', {})

    expect(font_metric_cache.instance_variable_get(:@cache).size).to eq(1)
  end

  it 'caches different copies for different strings' do
    font_metric_cache.width_of('M', {})
    font_metric_cache.width_of('W', {})

    expect(font_metric_cache.instance_variable_get(:@cache).entries.size)
      .to eq 2
  end

  it 'caches different copies of the same string with different font sizes' do
    font_metric_cache.width_of('M', {})

    document.font_size 24
    font_metric_cache.width_of('M', {})

    expect(font_metric_cache.instance_variable_get(:@cache).entries.size)
      .to eq 2
  end

  it 'caches different copies of the same string with different fonts' do
    font_metric_cache.width_of('M', {})

    document.font 'Courier'
    font_metric_cache.width_of('M', {})

    expect(font_metric_cache.instance_variable_get(:@cache).entries.size)
      .to eq 2
  end
end