stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

string.inspect should print non-byte bit strings with Gleam syntax

Open lpil opened this issue 10 months ago • 8 comments

     Values were not equal
     expected: "<<251, 3:size(3)>>"
          got: "//erl(<<251,3:3>>)"

lpil avatar Aug 15 '23 10:08 lpil

Is this the syntax for non byte aligned bit strings? I am not sure how to detect this or what to detect (too little experience with erlang/elixir binary syntax)

inoas avatar Aug 15 '23 13:08 inoas

We'd need to swap is_binary for is_bitstring here, and change the logic slightly to be able to print non-byte aligned bit strings.

https://github.com/gleam-lang/stdlib/blob/a5519c33086b3467039dd6d7178da95630d1a68b/src/gleam_stdlib.erl#L384

lpil avatar Aug 20 '23 07:08 lpil

How would I reproduce this?

markholmes avatar Aug 28 '23 20:08 markholmes

You could call string.inspect on <<251, 3:size(3)>>

lpil avatar Aug 30 '23 10:08 lpil

Is this the correct test?

@target(erlang)
pub fn inspect_erlang_non_byte_aligned_bit_string() {
  <<251, 3:size(3)>>
  |> string.inspect
  |> should.equal("<<251, 3:size(3)>>")
}

... because this already passes for me on gleam 0.30.5 and otp 26

inoas-nbw avatar Sep 04 '23 13:09 inoas-nbw

Yes that looks right. How did I get that string then? 🤔

Let's add that test to the repo to be sure.

lpil avatar Sep 04 '23 13:09 lpil

the test function above misses the test suffix. the error is as you described.

what about js?

gleam/string_test.inspect_erlang_non_byte_aligned_bit_string_test: Needs to be a byte size3

inoas avatar Sep 04 '23 13:09 inoas

It's not supported on JS currently so can't test there.

lpil avatar Sep 06 '23 09:09 lpil